Tried to show 3 notification in cluster format. As per the doc, I added the setGroupSummary(true) property for the first notification.But in the result i have got only two notification. The notification which is added the GroupSummary property is not visible.
NotificationCompat.Builder firstNotification = createNotification(context,"1.Message","Here you go 1");
firstNotification .setGroupSummary(true);
firstNotification .setGroup("KEY_NOTIFICATION_GROUP");
NotificationCompat.Builder secondNotifi = createNotification(context,"2.Message","Here you go 2");
secondNotifi .setGroup("KEY_NOTIFICATION_GROUP");
NotificationCompat.Builder thirdNotifi= createNotification(context,"3.Message","Here you go 3");
thirdNotifi.setGroup("KEY_NOTIFICATION_GROUP");
Here the notification trigger,
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,firstNotification .build());
notificationManager.notify(1,secondNotifi .build());
notificationManager.notify(2,thirdNotifi.build());
And the result is,
I want to show all three notification in the cluster format without missing.
Any help will be really appreciated.
You should check the following answer :
setgroup() in notification not working
You have to create a separate group notification and set the group summary flag true only for that, and that becomes the parent notification that bundles other notifications with the same group key within itself.
setGroupSummary's purpose is to support API levels below Nougat. On Android 7.0 and higher, it shows a normal group and just uses the on click behavior (setContentIntent) and details like the summary text of the summary notification.
On Android 7.0 and lower, it shows your summary notification as a replacement for all the other notifications the group contains.
Android 7 makes a decision regarding summary notification is shown by itself. So, you want see it unless system decides that it needs to be displayed.
Solution: create a dedicated summary notification.
Related
I have made an android app (Android Studio / Java) that checks a website for content and stores it in an sqlite DB. If new content is fetched that is not stored in DB, it shows a notification with the new content for user to notice.
That's working fine, although if user does not read/open/dismiss that notification, the next notification will update current one and replace its content with new data. This is wanted behavior, because I don't want the user to receive many notifications for the same thing, so I'm using the same notification id.
This introduces a problem though, if user checks the notification now, he will see the second fetched data, but won't be aware of the existence of the first fetched data.
So, what I'm trying to do is to append to the notification's content, so that both first and second fetched data are shown.
I tried the "inboxStyle" notifications that allow for new lines to be added, but it seems to be working only for setting many lines at the time notification is created and not for appending lines to an existing notifications.
I know that I can do that by storing what user has seen and what not, whether a notification was opened, etc, but this seems too much hassle for a simple thing, there must be an easier way to achieve it.
The expected behavior would be to either be able to append the message of existing notifications, or be able to fetch the message of an existing notification (by id) and then manually append to it and push the updated notification.
If that's not clear enough, the expected outcome is:
Issue the first notification with message "Test message 1"
Issue second notification using the same notification-id with message "Test message 2" that would NOT overwrite "Test message 1" but rather keep that message and append to it, so that the notification's message would now be "Test message 1 {newline-here} Test message 2" (or even better reversed so that the last message is shown on top).
Thank you in advance!
I was looking for a solution myself. I managed to do something that works but I'm quite sure there are other elegant solutions : I'm checking whether or not similar notification has been displayed. If so, I get the previous content and append it to the new notification content before publishing.
First, make sure that the notifications you want to assemble have the same uniqueID
This way, when you receive your 2nd, 3rd notification, you can easily match the one you are creating with the ones already displayed.
Note : This code works only on API 23 and higher.
String message = null;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
for (StatusBarNotification notification: notifications) {
if (notification.getId() == uniqueID) {
// You have a match
Bundle extras = notification.getNotification().extras;
message = extras.getCharSequence(Notification.EXTRA_TEXT).toString();
break;
}
}
}
The easy part : Now that you have the previous notification text, you have to append it to the new notification text before publishing
String newMessage = message + remoteMessage.getData.get("text");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),
channelID)
.setStyle(new NotificationCompat.BigTextStyle().bigText(newMessage))
.setContentTitle("Title")
.setContentText(notification.getMessage())
.setAutoCancel(true);
Send data message for app notification via FCM.
I have various FCM data types, and each type has its own action separate way.
Everything works fine, if I send only one message. App could handle exactly what I want.
But send FCM more than twice, (for example, [FCM - data for action 1] then [FCM - data for action 2] ) something goes wrong.
First, I want to show it separately, but second one overlay the first one.
Second, set 'First question' aside, after I click the message that contains the second one, it works for [ action 1 ] that the first one aimed.
So... I want to solve these problems. Or at least one. ( if first one is solved, second solve naturally )
Thx in advance.
did you use NotificationManager for displaying the notifications?
Then try this
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(), notifBuilder.build());
see the
(int) System.currentTimeMillis()
that's how i make the unique id for each notifications.
Hope that helps, thanks
If you want notification seperate, you need define diffrent id for notificaton:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(<Unique notification id here>, notifBuilder.build());
I'm trying to show a notification to the user in my Android application. I'm running two emulators at the moment. One is running at API level 16, and the other one is running at API level 25. When I send a notification to the application, the notification will only show up on the emulator which is running API level 25. I want to be able to show the notification on the emulator which is running API level 16 as well.
This is my code to show the notification to the user:
private void basicNotification(String title, String body, String data, Context context) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_monetization_on)
.setContentTitle(title)
.setContentText(body);
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(0, mBuilder.build());
}
Explanation parameters:
title = The title of the notification.
body = The body of the notification.
data = The data of the notification (not being used yet).
context = This is the context which I'm using to show the notification. This context is received from a class which extends FirebaseMessagingService.
When I execute the code above, I won't receive any errors. The only thing which will come up is in the logcat. The following line will appear:
05-11 07:47:42.169 1606-1758/system_process D/ConnectivityService: handleInetConditionHoldEnd: net=0, condition=100, published condition=100
I am not sure what I'm doing wrong right now. I used the official documentation from Android to make this method. If you need more information to solve the problem just let me know :)
So, after trying a few things, I found out I made a terrible mistake. The notification was showing up, but only in the drop down menu. For anyone who struggles with this, try to check your drop down menu if your notification is in there.
Related answer: https://stackoverflow.com/a/43809420/4653908
When I use WhatsApp or Telegram and receive N messages, the notification on Android will show "N new messages" (and it can be expanded).
Telegram shows notification with contentText "7 new messages" on Android. I have achieved this successfully.
However, on my Pebble Time, the last notification is the string of the last message, not "7 new messages".
On Pebble, it shows the 7th (last) message (the censored part is phone number). This is what I want.
I am trying to develop similar feature but have not succeeded. The notification on my Android displays correctly ("N new messages") but on Pebble Time, it's identical (also "N new messages", I want it to be the Nth message).
My app on Pebble. This is NOT what I want.
I have tried to call .notify twice (one contains "N new messages" and the other contains last message) and immediately .cancel the latter but Pebble Time only shows the first one.
If I don't call .cancel on the second notification, my Pebble will show what I want BUT there will be 2 notifications on both Android and Pebble (which I don't want).
How do I achieve similar feature like WhatsApp and Telegram?
Update:
This is the snippet I use (I have used different notification ID)
NotificationCompat.Builder nb = new NotificationCompat.Builder();
...
nb.setContentText("2 new messages");
notificationManager.notify(1, nb.build());
nb.setContentText("B");
notificationManager.notify(2, nb.build());
notificationManager.cancel(2);
Because you use same id for two notification item. you can try with different id for each show notification item.
From Android docs: https://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification)
id: An identifier for this notification unique within your
application.
How can I specify some custom logo and styling information to text. Or some custom action buttons just like music player notification in Android.
PS : I have successfully configured GCM with my app and is able to receive notification from GCM servers. And app is developed using cordova
You should read more about rich notifications in Android. From official documentation:
To have a notification appear in an expanded view, first create a NotificationCompat.Builder object with the normal view options you want. Next, call Builder.setStyle() with an expanded layout object as its argument.
Please see example below:
NotificationCompat.BigTextStyle textDiv = new NotificationCompat.BigTextStyle();
textDiv.bigText(bodyMessage);
notification = new NotificationCompat.Builder(context)
.setTicker(bodyMessage)
.setContentTitle(titleMessage)
.setSmallIcon(notificationIcon)
.setStyle(textDiv)
.setPriority(1)//priority high
.setVibrate(new long[0])
.build();
More info you could see in official documentation here and here