How to Display Multiple Notifications in Android

How to display multiple notifications in android

I solved my problem like this...

/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message,
String keys, String msgId, String branchId) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationCompat.Builder nBuilder;
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
nBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Smart Share - " + keys)
.setLights(Color.BLUE, 500, 500).setContentText(message)
.setAutoCancel(true).setTicker("Notification from smartshare")
.setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
.setSound(alarmSound);
String consumerid = null;
Integer position = null;
Intent resultIntent = null;
if (consumerid != null) {
if (msgId != null && !msgId.equalsIgnoreCase("")) {
if (key != null && key.equalsIgnoreCase("Yo! Matter")) {
ViewYoDataBase db_yo = new ViewYoDataBase(context);
position = db_yo.getPosition(msgId);
if (position != null) {
resultIntent = new Intent(context,
YoDetailActivity.class);
resultIntent.putExtra("id", Integer.parseInt(msgId));
resultIntent.putExtra("position", position);
resultIntent.putExtra("notRefresh", "notRefresh");
} else {
resultIntent = new Intent(context,
FragmentChangeActivity.class);
resultIntent.putExtra(key, key);
}
} else if (key != null && key.equalsIgnoreCase("Message")) {
resultIntent = new Intent(context,
FragmentChangeActivity.class);
resultIntent.putExtra(key, key);
}.
.
.
.
.
.
} else {
resultIntent = new Intent(context, FragmentChangeActivity.class);
resultIntent.putExtra(key, key);
}
} else {
resultIntent = new Intent(context, MainLoginSignUpActivity.class);
}
PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (notify_no < 9) {
notify_no = notify_no + 1;
} else {
notify_no = 0;
}
nBuilder.setContentIntent(resultPendingIntent);
NotificationManager nNotifyMgr = (NotificationManager) context
.getSystemService(context.NOTIFICATION_SERVICE);
nNotifyMgr.notify(notify_no + 2, nBuilder.build());
}

How to configure notification channels to show multiple notifications

Found the problem.
It's when calling notify() in the AlertReceiver class. I solved it by generating a unique id when calling it.

public void onReceive(Context context, Intent intent) {
NotificationHelper notificationHelper = new NotificationHelper(context);
int id = (int) System.currentTimeMillis(); //this is the fix

//add transactions reminder
NotificationCompat.Builder nb1 = notificationHelper.getChannel1Notification(
notificationHelper.getString(R.string.trans_notify_title), notificationHelper.getString(R.string.notification_msg)
);
notificationHelper.getManager().notify(id, nb1.build());

//due and overdue bills
NotificationCompat.Builder nb2 = notificationHelper.getChannel2Notification(
notificationHelper.getString(R.string.bills_notify_title), notificationHelper.getString(R.string.notification_msg)
);
notificationHelper.getManager().notify(id, nb2.build());

How to display multiple notification at the same time

You need to pass different notification id for each Notification . If you pass same id (i.e., 0 in your case), the existed notification will be updated with the new data

So change this:

  notificationManager.notify(0, notification);

to , e.g., Set some unique id int notificationId = 0x10;

notificationManager.notify(++notificationId, notification);  

ID - If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

How to display multiple notification as a group?

It will generate notification with multiple messages like Gmail

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.quemark1)
.setContentTitle("Title")
.setContentText("New Message received");
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();

inboxStyle.setBigContentTitle("doUdo");

// Add your All messages here or use Loop to generate messages

inboxStyle.addLine("Messgare 1");
inboxStyle.addLine("Messgare 2");
.
.
inboxStyle.addLine("Messgare n");

mBuilder.setStyle(inboxStyle);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

stackBuilder.addNextIntent(intent);

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(pIntent);

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
mBuilder.setAutoCancel(true);
mNotificationManager.notify(0, mBuilder.build());

How to display multiple notifications on different days?

Thanks to @Mike M's comment the solution is:

Calendar c1 = Calendar.getInstance();
c1.set(2020,5,15,0,0,0);
Calendar c2 = Calendar.getInstance();
c2.set(2020,5,20,0,0,0);



Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
intent.putExtra(Intent.EXTRA_TEXT, "We remind you that you can request a process now");
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);

am.set(AlarmManager.RTC_WAKEUP, c1.getTimeInMillis(), pendingIntent);

Intent intent2 = new Intent(MainActivity.this, AlarmReceiver.class);
intent.putExtra(Intent.EXTRA_TEXT, "We remind you that you can request a process thats end in 10 days");
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(MainActivity.this, 1,intent2, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am1 = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);

am1.set(AlarmManager.RTC_WAKEUP, c2.getTimeInMillis(), pendingIntent1);

Class ALarmReceiver

public class AlarmReceiver extends BroadcastReceiver {

int id = 0;
String text = intent.getStringExtra(Intent.EXTRA_TEXT);
@Override

public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.notification)
.setContentTitle("Notification")
.setContentText(text)
.setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})

notificationManager.notify(id, mNotifyBuilder.build());

}

Android push notification : Multiple notifications displays same data

Pass unique id here

 PendingIntent contentIntent = PendingIntent.getActivity(ctx,id,
intent, PendingIntent.FLAG_UPDATE_CURRENT);

Displaying multiple notifications at user-defined times

You implement notification part. For notify user at Specific time you should set AlarmManager. I paste whole code you need then explain each part:

public class AlarmReceiver extends WakefulBroadcastReceiver {

AlarmManager mAlarmManager;
PendingIntent mPendingIntent;

@Override
public void onReceive(Context context, Intent intent) {

int mReceivedID = Integer.parseInt(intent.getStringExtra(AddReminderActivity.EXTRA_REMINDER_ID));

// Get notification title from Reminder Database
ReminderDatabase rb = new ReminderDatabase(context);
ApiReminderModel reminder = rb.getReminder(mReceivedID);
String mTitle = reminder.getName();

// Create intent to open ReminderEditActivity on notification click

// handling when you click on Notification what should happen
Intent editIntent = YourActivity.createActivity(context, reminder.getChannelId(), reminder.getStartTime());
PendingIntent mClick = PendingIntent.getActivity(context, mReceivedID, editIntent, PendingIntent.FLAG_UPDATE_CURRENT);

// Create Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_logo))
.setSmallIcon(R.drawable.ic_logo)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setTicker(mTitle)
.setContentText(mTitle)
.setContentIntent(mClick)
.setSound(ringtoneUri)
.setAutoCancel(true)
.setOnlyAlertOnce(true);

NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(mReceivedID, mBuilder.build());
}

public void setAlarm(Context context, Calendar calendar, int ID) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

// Put Reminder ID in Intent Extra
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra(AddReminderActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

// Calculate notification time
Calendar c = Calendar.getInstance();
long currentTime = c.getTimeInMillis();
long diffTime = calendar.getTimeInMillis() - currentTime;

// Start alarm using notification time
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + diffTime, mPendingIntent);

// Restart alarm if device is rebooted
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}

public void cancelAlarm(Context context, int ID) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

// Cancel Alarm using Reminder ID
mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AlarmReceiver.class), 0);
mAlarmManager.cancel(mPendingIntent);

// Disable alarm
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}

As you see we have setAlarm which notify users at specific time. I also pass calendar instance which is initiated before ( assign a time which user should be notify).

 Calendar mCalendar = Calendar.getInstance();
mCalendar.add(Calendar.DAY_OF_YEAR, 7);// assume you want send notification 7 days later

new AlarmReceiver().setAlarm(getApplicationContext(), mCalendar, id);

We have another method cancelAlarm. If you want to delete a Alaram just pass unique ID of Alarm (Which already used for creation of Alarm).

Also don't forget to add this Service to your AndroidManifest.xml:

<receiver android:name=".service.AlarmReceiver" />

There is only on thing remain When you reboot your device you should set AlarmsManager again so you need a BootRecivier service.

Multiple notifications and only show the first one in Android

1) PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, i, 0); 
..............Your code ........................
nm.notify(1, noti);
2)PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 2, i, 0);
..............Your code ........................
nm2.notify(2, noti);
3)PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 3, i, 0);
..............Your code ........................
nm3.notify(3, noti);
4)................same as above
5)................same as above

Display multiple notifications that have been sent from Firebase on activity

It seems like you are adding the same ID_SMALL_NOTIFICATION. It should change every time if you want to show multiple notifications:

public void showSmallNotification(String title, String message, Intent intent) {
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mCtx,
ID_SMALL_NOTIFICATION,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx,"channelId");
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();

notification.flags |= Notification.FLAG_AUTO_CANCEL;

NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(getNotificationId(), notification);}

Change ID_SMALL_NOTIFICATION with getNotificationId()

private static int getNotificationId() {
Random rnd = new Random();
return 100 + rnd.nextInt(9000);
}


Related Topics



Leave a reply



Submit