I have multiple Notification but, the others are not showing up if the time is up, any Idea how can I fix this every notification have unique data tried this one but not working Multiple Notifications not showing in android
//Schedule alarm notification
private void scheduleNotification(Notification notification, long delay) {
Intent notificationIntent = new Intent(getContext(), MyNotificationPublisher.class);
notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION, notification);
notificationIntent.putExtra(MyNotificationPublisher.NOTIF_CONTENT, title);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
assert alarmManager != null;
alarmManager.set(AlarmManager.RTC_WAKEUP, delay, pendingIntent);
Log.d(TAG, "scheduleNotification: Notification set successfully!");
}
//Build notification
private Notification getNotification(String content) {
//on notification click open MainActivity
Intent intent = new Intent(getContext(), SchedulerFragment.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), default_notification_channel_id);
builder.setContentTitle("ToDo Reminder");
builder.setContentText(content);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_stat_name);
builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
return builder.build();
}
O NotificationPublisher
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(SchedulerFragment.NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
}
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
String con = intent.getStringExtra(NOTIF_CONTENT);
assert notificationManager != null;
notificationManager.notify(id, notification);
You can try the following code,This method is used to generate notification. If different notifications have different channelId
, multiple notification can be generated at the same time.
private void initNotification(Context context,String channelId,String channelName,String title,String content){
Intent activityIntent = new Intent(context,MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(context,0,activityIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationChannel channel = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(context,channelId)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentIntent(pi)
.build();
manager.notify(Integer.parseInt(channelId),notification);
}
Ensure that the value of String channelID
is different to generate a multiple notification.
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.