在我的应用程序中,用户必须添加多个通知,但我的问题是当用户添加新通知时旧的通知被删除或取消。我认为它来自pendingIntent,应该是什么相应的标志用吗?
这是我的一些代码:
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 :(得分:0)
如果为每个PendingIntent
通知指定不同的requestCode,则通知不会互相取消。
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()
答案 1 :(得分:0)
您需要在应用程序build.gradle文件中添加如下所示的依赖项:
Java:实现“ androidx.preference:preference:1.1.1”
Kotlin:实现“ androidx.preference:preference-ktx:1.1.1”
然后在您的片段中执行类似的操作。如果您在活动中,则不需要requireActivity()。
`
val sharedPreferences = requireActivity().getSharedPreferences("Notification_data", Context.MODE_PRIVATE)
val customRequestCode = sharedPreferences.getInt("customRequestCode", 0) + 1
sharedPreferences.edit().putInt("customRequestCode", customRequestCode).apply()
`