0

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.

Sagar Suri
  • 111
  • 2
  • 10

1 Answers1

0

According to the documentation -

ELAPSED_REALTIME_WAKEUP

Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep), which will wake up the device when it goes off.

Thus, ELAPSED_REALTIME_WAKEUP will set off the alarm after the device boots whereas RTC_WAKEUP will trigger the alarm according to the time of the clock.

I use

manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);

for setting off alarms in my project, and it works fine.

Kunal Chawla
  • 1,236
  • 2
  • 11
  • 24
  • Are you getting the correct Calendar time in Log when setting the alarm ? – Kunal Chawla Jul 01 '17 at 07:51
  • I even tried for every one minute. But still not getting the notification. AlarmManager is not able to trigger the Broadcast Receiver. – Sagar Suri Jul 01 '17 at 07:56
  • There is a statement `alarmManager.cancel(pendingIntent);` in your code. Can you try after removing this please ? I see no need of calling cancel when setting an alarm in this case. – Kunal Chawla Jul 01 '17 at 08:00
  • I have updated the logcat. Now the application is crashing. I removed `alarmManage.cancel(pendingIntent)` – Sagar Suri Jul 01 '17 at 08:14
  • Is your Broadcast Receiver in a separate class or is it in the Activity class ? From the Manifest it seems its within the MenuActivity class. Please create a new public class for it and then try. Your receiver is otherwise inner and must be registered within the activity to listen only when activity is active. – Kunal Chawla Jul 01 '17 at 08:17
  • yes its inside my Activity. I have some reasons to keep it inside the Activity. How can I initilize it inside the Activity. – Sagar Suri Jul 01 '17 at 08:26
  • Okay. Check [this](https://stackoverflow.com/a/4854830/4768284) out. You'll have to register and unregister for broadcasts within your activity. rest remains same. – Kunal Chawla Jul 01 '17 at 08:28
  • Thank you so much. Now after long time I am getting the notification. Actual problem was to the `alarm.cancel()` and i added `calendar.set(Calendar.AM_PM,Calendar.PM);` – Sagar Suri Jul 01 '17 at 08:38
  • Great ! Glad I could be of some help. :) – Kunal Chawla Jul 01 '17 at 08:39