Android:如何修复通知的递增setNumber()?

Android:如何修复通知的递增setNumber()?,android,android-notifications,notificationmanager,jobintentservice,Android,Android Notifications,Notificationmanager,Jobintentservice,我使用从BroadcastReceiver启动的JobIntentService向用户发送到期日临近的通知。当另一个到期日的下一个通知临近时,我只想更新现有通知并将setNumber()指示符增加+1。第一个通知将“TotalMessages”变量正确递增+1,并且setNumber()在通知下拉对话框中显示“1”。下一个通知将正确触发,但setNumber()不会增加+1到“2”。它保持在“1” 我错过了什么 public class AlarmService extends JobInten

我使用从
BroadcastReceiver
启动的
JobIntentService
向用户发送到期日临近的通知。当另一个到期日的下一个通知临近时,我只想更新现有通知并将setNumber()指示符增加+1。第一个通知将“TotalMessages”变量正确递增+1,并且setNumber()在通知下拉对话框中显示“1”。下一个通知将正确触发,但setNumber()不会增加+1到“2”。它保持在“1”

我错过了什么

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());
    }
  }
}   
每次从
BroadcastReceiver
启动
JobIntentService
时,该值初始化为0

解决方案之一是将totalMessage存储在SharedReferences中,并在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();

您可以在代码中的通知生成器之前插入它。

Ok。我想在用户单击通知对话框后将totalMessages重置为零。如果我使用编辑器,我应该重置为0(零)还是null?下一个通知将使用上面的代码初始化为0(零)。此外,在上面的代码中使用getSharePreferences()而不是使用getPreferences()是否有一个主要的好处?您可以让通知触发另一个
BroadcastReceiver
,它将首选项中的
totalMessages
设置为零
getSharedReferences()
允许指定标识符,因此您可以从其他地方访问它,这在您的情况下很有用,因为第二个
BroadcastReceiver
可以访问它以将其设置回零
getPreferences()
不允许您这样做,只能由创建它的活动/服务访问。我使用的所有通知代码都在JobIntentService中。那么,我是否将第二个BroadcastReceiver设置为PendingEvent的JobIntentService中的静态内部类?您可以创建第二个BroadcastReceiver,就像触发此JobIntentService的第一个BroadcastReceiver一样。您可以有针对Pending帐篷内第二个broadcastReceiver的意图。我不确定BroadcastReceiver作为静态内部类是否有效。希望其他人能帮你。
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();