【问题标题】:Android: how to fix incrementing setNumber() for a Notification?Android:如何修复通知的递增 setNumber()?
【发布时间】:2018-05-14 06:31:31
【问题描述】:

我使用从BroadcastReceiver 启动的JobIntentService 向用户发送到期日期临近的通知。当另一个到期日的下一个通知临近时,我只想更新现有的通知并将 setNumber() 指示器增加 +1。第一个通知将“totalMesssages”变量正确增加 +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());
    }
  }
}   

【问题讨论】:

    标签: android android-notifications notificationmanager jobintentservice


    【解决方案1】:
    private int totalMessages = 0;
    

    每次从BroadcastReceiver 启动JobIntentService 时,它都会被初始化为0。

    其中一种解决方案是将 totalMessage 存储在 SharedPreferences 中并在您的 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();
    

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

    【讨论】:

    • 好的。在用户单击通知对话框后,我想将 totalMessages 重置为零。如果我使用编辑器,我应该重置为 0(零)还是 null?下一个通知将使用您上面的代码初始化为 0(零)。
    • 另外,在上面的代码中使用 getSharePreferences() 比使用 getPreferences() 有很大的好处吗?
    • 您可以让通知触发另一个BroadcastReceiver,它将首选项中的totalMessages 设置为零。 getSharedpreferences() 允许指定标识符,因此您可以从其他地方访问它,这在您的情况下很有用,因为第二个 BroadcastReceiver 可以访问它以将其设置回零。 getPreferences() 不允许您这样做,并且只能由创建它的活动/服务访问。 getSharedPreferences vs getPreferences
    • 我使用的所有通知代码都在 JobIntentService 中。那么我是否将第二个 BroadcastReceiver 设置为 JobIntentService 中的 PendingIntent 的静态内部类?
    • 您可以创建第二个广播接收器,就像您拥有的第一个触发此 JobIntentService 的广播接收器一样。您可以有一个以pendingIntent 中的第二个broadcastReceiver 为目标的意图。我不确定作为静态内部类的 BroadcastReceiver 是否可以工作。希望其他人可以帮助您。
    猜你喜欢
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多