in my application the user has to add multiple notifications , but my problem is when the user add the new notification the old one deleted or canceled .And I think it's from the pendingIntent , what the appropriate flag should i use ?
here is some of my code :
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;
If you specify a different requestCode for each PendingIntent
notifications won't cancel each other.
PendingIntent.getBroadcast(this,
unique_code , intent, PendingIntent.FLAG_UPDATE_CURRENT);
Edit
int uniqueCode = sharedPreferences.getInt("unique_code",0) + 1;
sharedPreferences.edit().putInt("unique_code",uniqueCode).apply()
You would need to add the dependency as shown below in your app build.gradle file:
Java: implementation "androidx.preference:preference:1.1.1"
Kotlin: implementation "androidx.preference:preference-ktx:1.1.1"
Then do something like this in your fragment. You don't need requireActivity() if you are inside an activity.
`
val sharedPreferences = requireActivity().getSharedPreferences("Notification_data", Context.MODE_PRIVATE)
val customRequestCode = sharedPreferences.getInt("customRequestCode", 0) + 1
sharedPreferences.edit().putInt("customRequestCode", customRequestCode).apply()
`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.