I am using AlarmManager to trigger a notification once per day. I have implemented below logic to trigger notification. But its not working. Please rectify my fault.
I am calling the below code in my MainActivity's onCreate() method:
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(this, MyStartServiceReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmIntent.setData(Uri.parse("custom://" + System.currentTimeMillis()));
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
Calendar now = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 0);
if (now.after(calendar)) {
Log.e("AlarmManager", "Added a day");
calendar.add(Calendar.DATE, 1);
}
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Here is my Broadcast Receiver:
public class MyStartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("AlarmReceiver", "Started");
Intent intent1 = new Intent(context, AlarmService.class);
intent1.setData(Uri.parse("custom://" + System.currentTimeMillis()));
context.startService(intent1);
}
}
Here is my Service where the notification will be triggered:
public class AlarmService extends IntentService {
public AlarmService() {
super("AlarmService");
}
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private static int NOTIFICATION_ID = 1;
Notification notification;
@Override
protected void onHandleIntent(Intent intent) {
Log.e("Service", "I ran");
Context context = this.getApplicationContext();
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, MenuActivity.class);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Resources resources = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
notification = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
.setTicker("ticker value")
.setPriority(8)
.setSound(soundUri)
.setContentTitle("Sun Rise")
.setContentText("Time to draw kolam").build();
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xFFFFA500;
notification.ledOnMS = 800;
notification.ledOffMS = 1000;
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Log.i("notif", "Notifications sent.");
}
}
At Last here is my Manifest.xml:
<receiver
android:name=".MenuActivity$MyStartServiceReceiver"
android:enabled="true"/>
<service
android:name=".services.AlarmService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</service>
<receiver
android:name=".BootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
UPDATE:
My Logcat:
java.lang.RuntimeException: Unable to instantiate receiver com.ignite.a01hw909350.kolamdemo.MenuActivity$MyStartServiceReceiver: java.lang.InstantiationException: java.lang.Class<com.ignite.a01hw909350.kolamdemo.MenuActivity$MyStartServiceReceiver> has no zero argument constructor
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2753)
at android.app.ActivityThread.access$1800(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1450)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5609)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.InstantiationException: java.lang.Class<com.ignite.a01hw909350.kolamdemo.MenuActivity$MyStartServiceReceiver> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2748)
at android.app.ActivityThread.access$1800(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1450)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5609)
at java.lang.reflect.Method.invoke(Native Method)
My AlarmManager is not able to trigger the broadcast receiver.