0

私のアプリケーションでは複数の通知を追加する必要がありますが、ユーザーが新しい通知を追加したときに古い通知が削除または取り消されます。フラグを使うべきですか?あなたがお互いをキャンセルしません各PendingIntent通知の異なるrequestCodeを指定した場合新しい通知は古いアプリケーションをキャンセルします

Calendar calendar = Calendar.getInstance(); 
    Intent intent; 
    PendingIntent pendingIntent; 
    AlarmManager alarmManager; 
    long futureInMillis; 
    switch (type) { 
     case SCHEDULE_BY_DAYS: 
      intent = new Intent(this, NotificationReceiver.class); 
      intent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1); 
      intent.putExtra(NotificationReceiver.NOTIFICATION, getNotification("WAKEP UP days !!")); 
      pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
      alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
      alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
      break; 
     case SCHEDULE_BY_HOURS: 

      futureInMillis = SystemClock.elapsedRealtime() + (value * 600000); 
      intent = new Intent(this, NotificationReceiver.class); 
      intent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1); 
      intent.putExtra(NotificationReceiver.NOTIFICATION, getNotification("WAKEP UP hours")); 
      pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

      alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
      alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); 
      break; 

答えて

0

は、ここに私のコードの一部です。

PendingIntent.getBroadcast(this,UNIQUE_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

編集

int uniqueCode = sharedPreferences.getInt("unique_code",0) + 1; 
sharedPreferences.edit().putInt("unique_code",uniqueCode).apply() 
+0

私はユニークなコードが含まれるか、理解していませんでしたか? – user3604741

+0

はちょうど上にカウントされます。最初の通知は1を取得し、2番目の通知は2などを取得します。これは一意でなければならず、これを確実にする最も簡単な方法です。 – F43nd1r

+0

これはどのように動作するのかを理解するためのサンプルコードを私に与えることができますか?同じIDを取り消す必要があります。 – user3604741

関連する問題