Search code examples
androidnotificationsalarmmanager

Android Future Notification Not Showing


I want my app to notify the user daily and I am using an AlarmManager to implement that. However, the notifications are not showing up. Also, I do not have any errors or exceptions.

This is the part of my code that sets the AlarmManager(The repeating interval is 1000ms for testing, I will change it in the actual program):

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        Intent intent1 = new Intent(this, Notify.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000, pendingIntent);

This is onReceive() in Notify class, which extends BroadcastReceiver:

  public void onReceive(Context context, Intent intent) {
    NotificationCompat.Builder n=new NotificationCompat.Builder(context);
    n.setAutoCancel(true);
    n.setSmallIcon(R.mipmap.ic_launcher);
    n.setTicker("ticker");
    n.setWhen(System.currentTimeMillis());
    n.setContentTitle("title");
    n.setContentText("text");
    Intent it=new Intent(context,ListAll.class);
    PendingIntent pi=PendingIntent.getBroadcast(context,0, it,0);
    n.setContentIntent(pi);
    ((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(uniqueID,n.build());
  }

And I have the following added in my Manifest.xml:

   <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
   <receiver android:name=".Notify" android:process=":remote" />

Solution

  • I'm pretty sure that your user's device will need to NOT reboot at all or else your alarm will go away unless they re-launch your app, and your app knows to set the alarm again.

    Documentation found:

    By default, all alarms are canceled when a device shuts down. To prevent this from happening, you can design your application to automatically restart a repeating alarm if the user reboots the device. This ensures that the AlarmManager will continue doing its task without the user needing to manually restart the alarm.

    from the http://developer.android.com/training/scheduling/alarms.html page


    You are setting the alarm to fire every 1 second That might be the issue, I wouldn't be surprised if the internal code would just throw away alarms like that. (for sanity of the device+ battery)


    from the examples here: http://developer.android.com/training/scheduling/alarms.html#set You are missing lines like this, which you set 'what time' the alarm should go off.

    the 'time' from calendar.setTimeInMillis(System.currentTimeMillis()); has already passed when you tell the alarm manager to set a repeating alarm. Therefore I'm not sure the behavior it will give you, but its probably your undesired behavior that you are receiving. Try to tell the alarm to 'go off' in 5 minutes, and to repeat every 20 minutes. (like the example in the guide). See if that helps.

    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 30);
    

    I think you are conflating AlarmManager with AlarmClock.

    https://developer.android.com/reference/android/app/AlarmManager.html

    and

    https://developer.android.com/reference/android/provider/AlarmClock.html


    I don't think you need <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> at all, since you aren't writing an alarm clock app. you just want to 'use' alarms.

    This guide Should help you tiny up your code. Because you will need to set a broadcast receiver to be notified on android.permission.RECIEVE_BOOT_COMPLETED and this has code on how to do that. (to 'restart' your alarms each time the phone reboots)