Reputation: 13
I want to show firbase notification in only one notification but when per time i send push notification by console or my web service automatic create in the Different notifications. even when I don't use notification builder in my code, and android itself automatic create notification, Eventually I WANT create notification in Inbox style and display all message or title below them, thanks for per your responding and your help!!!
enter code here@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(MyApplication.LOG_TAG, "remote Message:" + remoteMessage.toString());
Log.e(MyApplication.LOG_TAG, "remote Message->(Data):" + remoteMessage.getData().toString());
Log.i(MyApplication.LOG_TAG, "FROM:" + remoteMessage.getFrom());
Log.i(MyApplication.LOG_TAG, "MessageID:" + remoteMessage.getMessageId());
Log.i(MyApplication.LOG_TAG, "MessageType:" + remoteMessage.getMessageType());
Log.i(MyApplication.LOG_TAG, "To:" + remoteMessage.getTo());
Log.i(MyApplication.LOG_TAG, "SentTime:" + remoteMessage.getSentTime());
Log.i(MyApplication.LOG_TAG, "Ttl:" + remoteMessage.getTtl());
Log.i(MyApplication.LOG_TAG, "CollapseKey:" + remoteMessage.getCollapseKey());
if (remoteMessage.getNotification() != null) {
fcm.setTag(remoteMessage.getNotification().getTag());
fcm.setTitle(remoteMessage.getNotification().getTitle());
fcm.setBody(remoteMessage.getNotification().getBody());
fcm.setBodyLocalizationKey(remoteMessage.getNotification().getBodyLocalizationKey());
fcm.setClickAction(remoteMessage.getNotification().getClickAction());
fcm.setColor(remoteMessage.getNotification().getColor());
fcm.setIcon(remoteMessage.getNotification().getIcon());
Log.i(MyApplication.LOG_TAG, "Notification:" + remoteMessage.getNotification());
Log.i(MyApplication.LOG_TAG, "Sound:" + remoteMessage.getNotification().getSound());
Log.i(MyApplication.LOG_TAG, "BodyLocalizationKey:" + remoteMessage.getNotification().getBodyLocalizationKey());
Log.i(MyApplication.LOG_TAG, "ClickAction:" + remoteMessage.getNotification().getClickAction());
Log.i(MyApplication.LOG_TAG, "Color:" + remoteMessage.getNotification().getColor());
Log.i(MyApplication.LOG_TAG, "Icon:" + remoteMessage.getNotification().getIcon());
Log.i(MyApplication.LOG_TAG, "Tag:" + remoteMessage.getNotification().getTag());
Log.i(MyApplication.LOG_TAG, "Title:" + remoteMessage.getNotification().getTitle());
Log.i(MyApplication.LOG_TAG, "Body:" + remoteMessage.getNotification().getBody());
}
if (remoteMessage.getData().size() > 0) {
Log.i(MyApplication.LOG_TAG, "Message data: " + remoteMessage.getData().toString());
Log.i(MyApplication.LOG_TAG, "Have field=" + remoteMessage.getData().containsKey("MessageID"));
try {
JSONObject data = new JSONObject(remoteMessage.getData().toString());
message = messageParser(data);
} catch (Exception e) {
Log.e(MyApplication.LOG_TAG, "Exception: " + e.getMessage());
}
MessageClass.LogConsole(message);
showNotification(message.getTitle)
}
public static void showNotification(String Message) {
Intent intent = new Intent(MyApplication.context, MyBroadcastReceiver.class);
PendingIntent broadcastIntent = PendingIntent.getBroadcast(MyApplication.context, 0, intent, 0);
Bitmap icon = BitmapFactory.decodeResource(MyApplication.context.getResources(), R.drawable.shna);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
MyApplication.preferenceManager.setNotification(Message);
String oldNotification = MyApplication.preferenceManager.getNotifications();
Log.e(MyApplication.LOG_TAG, "Notification:" + oldNotification);
List<String> messages = Arrays.asList(oldNotification.split("\\|"));
for (int i = messages.size() - 1; i >= 0; i--) {
inboxStyle.addLine(messages.get(i));
}
ShortcutBadger.applyCount(MyApplication.context, messages.size());
inboxStyle.setSummaryText(messages.size() + " More");
notification = new NotificationCompat.Builder(MyApplication.context)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(icon)
.setWhen(0)
.setCategory(Notification.CATEGORY_MESSAGE)
.setGroup(Notification.CATEGORY_MESSAGE)
.setAutoCancel(true)
.setDeleteIntent(broadcastIntent)
.setContentTitle("")
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
.setStyle(inboxStyle)
.build();
MyApplication.notificationManager.notify(0, notification);
}
public Message messageParser(JSONObject data) {
Message message = new Message();
try {
message.setTitle(data.getJSONObject("data").getString("Title"));
message.setDated(data.getJSONObject("data").getString("Dated"));
} catch (JSONException e) {
Log.e(MyApplication.LOG_TAG, "Json Exception: " + e.getMessage());
return null;
} catch (Exception e) {
Log.e(MyApplication.LOG_TAG, "Exception: " + e.getMessage());
return null;
}
return message;
}
}
Upvotes: 1
Views: 1818
Reputation: 37798
You could use the tag
for the notification
payload:
Identifier used to replace existing notifications in the notification drawer.
If not specified, each request creates a new notification.
If specified and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.
However, note that you won't be able to set the value for tag
when you send the message from the Firebase Console.
Or you could just bundle the notifications yourself.
Upvotes: 2