1

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());
    }
  }
}   
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
AJW
  • 1,578
  • 3
  • 36
  • 77

1 Answers1

2
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.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Waseem
  • 111
  • 2
  • Ok. I'd like to reset totalMessages back to zero after the Notification dialog is clicked on by the user. If I use the editor, should I reset to 0 (zero) or to null? The next Notification would use your code above to initialize to 0 (zero). – AJW May 14 '18 at 16:03
  • Also, is there a major benefit to using getSharePreferences() in your code above, rather than using getPreferences()? – AJW May 14 '18 at 16:23
  • You could have the notification trigger another `BroadcastReceiver` which sets the `totalMessages` in the preference to zero. `getSharedpreferences()` allows for specifying an identifier, so you can access it from elsewhere, this is useful in your case since the second `BroadcastReceiver` can access it to set it back to zero. `getPreferences()` does not allow you to do that, and can be accessed only by the activity/service that created it. [getSharedPreferences vs getPreferences](https://stackoverflow.com/q/33447290/9786551) – Waseem May 16 '18 at 12:04
  • All of the notification code I use is within a JobIntentService. So do I set up the second BroadcastReceiver as a static inner class within JobIntentService for the PendingIntent? – AJW May 16 '18 at 12:07
  • You can create the second broadcastreceiver just like the first one you have that triggers this JobIntentService. You can have an intent that targets the second broadcastReceiver inside pendingIntent. I am not sure if BroadcastReceiver as static inner class would work. Hope someone else could help you with that. – Waseem May 18 '18 at 10:14