Multiple Notifications to The Same Activity

Multiple notification click launching a same activity android

Solution was just adding a new ID for PendingIntent reference

private void sendNotificationForCancellation(String appointmentId, String title, String message) {
Random generator = new Random();
//AppController.getSharedPreferences().edit().putString(Constants.CALL_CASE_ID, notifObject.getCaseDetails().getCaseID()).commit();
Intent intent = new Intent(this, ActCallRequestsDetail.class);
intent.putExtra("appointment_id", appointmentId);
intent.putExtra("isAppIdAvailable", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this, generator.nextInt(), intent, PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_note_plus)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.app_icon))
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(generator.nextInt(), notificationBuilder.build());
}

Multiple notifications with multiple action

Seems that you don't use putExtra() correctly. It doesn't put two values, it puts one value by a key. So, you should build your Intents like this:

String ACTION_PAUSE = "pause";
String EXTRA_USER_NAME = "user_name";
Intent intentPause = new Intent(this, MyBroadcastReceiver.class);
intentPause.setAction(ACTION_PAUSE); // we put button action in a specific field, not in extras
intentPause.putExtra(EXTRA_USER_NAME, userName);

Then wrap this Intent with PendingIntent and attach to your notification:

PendingIntent pendingIntentPause = PendingIntent.getBroadcast(this, 0, intentPause, 0);

In your BroadcastReceiver first check action type, and only then get data from extras:

public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_PAUSE)) {
timerResume();
Log.d("Action:", action);
String userName = intent.getStringExtra(EXTRA_USER_NAME);
}
...
}

Android push notification : Multiple notifications displays same data

Pass unique id here

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

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);
}

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());
}

Android Multiple Notifications and with multiple intents

You create multiple intents that are mixed. I cleaned up the code (but did not test it)

    NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = ctx.getResources();

// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
String pass = text.getText().toString();
resultIntent.setData(new Uri.Builder().scheme("data")
.appendQueryParameter("text", "my text").build());
resultIntent.putExtra("title", pass);
resultIntent.putExtra("uid", i);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(
BitmapFactory.decodeResource(res,
R.drawable.ic_launcher))
.setTicker("Remember to " + text.getText())
.setWhen(System.currentTimeMillis()).setAutoCancel(true)
.setContentTitle(text.getText())
.setContentIntent(resultPendingIntent);

Notification n = builder.build();
n.flags = Notification.FLAG_NO_CLEAR;
nm.notify(i++, n);

text.setText(null);

How to implement Multiple notifications onclick in android using fcm?

Try to replace PendingIntent requestCode 0 with new Random().nextInt() as below

PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(), intent,
PendingIntent.FLAG_ONE_SHOT);


Related Topics



Leave a reply



Submit