Search code examples
androidandroid-notificationsnotificationmanagerjobintentservice

Android: how to fix incrementing setNumber() for a Notification?


I use a JobIntentService launched from a BroadcastReceiver to send the user a Notification that a due date is near. When the next Notification for another due date is near, I just want to update the existing Notification and increment the setNumber() indicator by +1. The first Notification increments the "totalMesssages" variable correctly by +1 and the setNumber() shows the "1" on the Notification dropdown dialog. The next Notification fires correctly but setNumber() does not increment by +1 to "2". It stays at "1".

What am I missing here?

public class AlarmService extends JobIntentService {

    static final int JOB_ID = 9999;
    private int totalMessages = 0;

    static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, AlarmService.class, JOB_ID, work);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {

    sendNotification();
    }

    private void sendNotification() {

        int notifyID = 1;

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);

        if (notificationManager != null) {
         notificationManager.createNotificationChannel(notificationChannel);
        }
    }

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
        .setDefaults(Notification.DEFAULT_ALL)
        .setSmallIcon(R.drawable.ic_announcement_white_24dp)
        .setContentText("")
        .setNumber(++totalMessages);

    Intent intent = new Intent(this, MainActivity.class);        
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(contentIntent);
    mBuilder.setAutoCancel(true);;

    if (notificationManager != null) {
        notificationManager.notify(notifyID, mBuilder.build());
    }
  }
}   

Solution

  • private int totalMessages = 0;
    

    This gets initialized to 0 each time JobIntentService is launched from the BroadcastReceiver.

    One of the solutions is to store the totalMessage in SharedPreferences and use it in your AlarmService.

    SharedPreferences sp = getApplicationContext().getSharedPreferences("preferences_name", Context.MODE_PRIVATE);
    int totalMessages = sp.getInt("total-messages", 0); //initialize to 0 if it doesn't exist
    SharedPreferences.Editor editor = sp.edit();
    editor.putInt("total-messages",++totalMessages);
    editor.apply();
    

    You could insert this just before the notification builder in your code.