Reputation: 47
I want to display multiple notifications that are already sent from Firebase (I am using server, php-not console) and when the application is closed and opened again, it can display multiple notifications. But I only got one notification and it displays the latest message when clicked, and I want the notifications stay in main activity.
TextView text = (TextView) findViewById(R.id.data);
if (text != null) {
Bundle extras = getIntent().getExtras();
StringBuilder keys = new StringBuilder();
if (extras != null) {
for (String key : extras.keySet())
keys.append(key + " = " + extras.getString(key) + "\n");
}
text.setText("" + keys.toString());
}
Firebase messaging service
private void sendPushNotification(JSONObject json) {
Log.e(TAG, "Notification JSON " + json.toString());
try {
JSONObject data = json.getJSONObject("data");
String title = data.getString("title");
String message = data.getString("message");
String imageUrl = data.getString("image");
MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("title",title);
intent.putExtra("message",message);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0/*request code*/,intent,PendingIntent.FLAG_ONE_SHOT);
if(imageUrl.equals("null")){
mNotificationManager.showSmallNotification(title, message, intent);
}
else{
mNotificationManager.showBigNotification(title, message, imageUrl, intent);
}
}
catch (JSONException e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
}
catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
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(ID_SMALL_NOTIFICATION, notification);
}
I'm sorry if this already asked before, but I already googled it and didn't find any similar case that I want, but maybe it is due to my limited vocabulary.
Upvotes: 2
Views: 1597
Reputation: 191
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);
}
Upvotes: 2