I want to group notifications into a summary.
I achieve this by having a single ID for all notifications. This way android will not create new notifications but update the existing one (reduced code):
Notification summaryNotification = new NotificationCompat.Builder(this)
.setGroupSummary(true)
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.InboxStyle()
.addLine(msg)
.setBigContentTitle("My App")
.setSummaryText("FooBar"))
.build();
mNotificationManager.notify(uuid, summaryNotification);
UUID is always the same so that the notification should be updated. However when a new notification arrives, setStyle seems to be overwritten.
This cause the old addLine(msg) to disappear. However I want the new message to be added without having some kind of notification manager server side.
Any ideas or suggestions?
I think you are misinterpreting the notification builder.
The NotificationCompat.Builder builds the complete notification, with all the content.
Reusing the same id simply tells the notification manager to replace an existing notification with the same id with the new one: (Source)
[...] update or create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the same ID you used previously. If the previous notification is still visible, the system updates it from the contents of the Notification object.
Thus addLine is not an operation that is performed on an existing notification, but on the new builder you created (which is empty at that time).
If you want to add a line to an existing notification with the inbox style, you will need to either
keep the original builder object, add lines as needed and resend the notification with the same id
create a new builder and add the old lines first, then the new one. You will need to store or retrieve the old lines from somewhere (depending on your application).
Related
Currently I have created group key for my android app so that the push notifications can be grouped together. My minimum target api level is 24 so I do not need to create any Summary Notification. What I am doing is something like this:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelID)
.setSmallIcon(R.drawable.applogo)
.setContentTitle(sourceName)
.setContentText(messageTitle)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(defaultSoundUri)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setGroup(GROUPID)
Thus I am creating the NotificationCompat Builder. The problem what I am facing is Notifications are being grouped together after the 4th one. The first three notifications are being displayed separately in the notification tray. When the fourth one is coming, they are grouped together. What I want to know is that is this the default behavior and if so then how can I change it like notifications will be grouped together when the 2nd one comes?
Notifications will be grouped after 4, this is the default behaviour since 24. And to trigger group notification less than 4 you need to create group summary notification.
I have an app where the user can receive multiple notifications for things they need to do. The user has a choice of making some of these notifications persistent (which I achieve by calling NotificationCompat.Builder.setOngoing). At least on my version of Android which is Nougat, when more than three notifications are posted by my app they get bundled together into one notification, which makes all of them dismissible to the user in one swipe. This makes the previously persistent notifications no longer persistent. Is there a way to programmatically instruct Android not to bundle my notifications?
This is the code I use to build the notification and display it:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(eventName + " " + notificationText)
.setDefaults(Notification.DEFAULT_ALL)
.setOnlyAlertOnce(true)
.setContentIntent(eventListPendingIntent);
if (goalInfo.goal.persistNotification) {
builder.setOngoing(true);
} else {
builder.setAutoCancel(true);
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(eventType.value(), builder.build());
Thanks, Nir
As per Google docs, notifications from the same app would be bundled automatically -
Note: If the same app sends four or more notifications and does not
specify a grouping, the system automatically groups them together.
So in your case, what you can do is , instead of system applying the default grouping, you can separate your notifications into two groups using a separate group key for the persistent notifications and one for the non-persistent ones.
Check the Google docs. The method Builder.setGroup() on NotificationBuilderCompat takes a string parameter which is the key.
There is a related method Builder.setGroupSummary which you should call on your Builder
Hope this is clear.
I am building a messaging application that notifies users when a new message comes in.
Because this could happen several times a day (or several times an hour), I don't want to continually throw new notifications. Instead, if the user has not dismissed a notification, I would like to update it with the number of new messages pending (following the "Stacking" design guideline).
In the Android documentation, there is an example of updating a notification with a number:
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
...
HOWEVER, this seems to assume that you are maintaining this number within your application and outside of the notification manager / builder. For a host of reasons, this is very inconvenient (and brittle) in the context of my application.
I would like to know - is there any way to read the current number assigned to a message (the equivalent of mNotifyBuilder.getNumber()) ?
FOLLOW-ON QUESTION: If reading the current number is not possible, is there a way to know from a running service if a notification has been cancelled or manually dismissed by the user ?
You notification is assigned with an ID. You can use this ID to cancel or update the same notification if it hasn't been cancelled by the user.
Straight from the documentation: "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."
See this post and that one.
At the Google I/O Bytes video How We Customized Google Apps for Android Wear (https://www.youtube.com/watch?v=o5cne6vK-eo), I saw that for the Wearable-customized Camera App, they add a button directly on the notification (not at the back of the notification as a new page which would happen if addAction or setContentAction is used).
Does any one know which API I need to use in order to do that? I don't think there are using a custom Activity for the first view cos it just looks like the first screen of Android Wear when there is at least one Notification. I've tried to find for it in the documentations but couldn't get it. I've tried setDisplayIntent which is suggested by others but it doesn't seems to be the same one.
Use WearableExtender.setContentAction(int) to add an action directly to a notification card. The int parameter refers to the index of the action you have added to the notification (using NotificationCompat.Builder.addAction(NotificationCompat.Action)). See Creating a Notification for more info on how to create notification for wearables.
The sample code you can download using the SDK manager contains a sample project Notifications (located under /samples/android-20/wearable/Notifications) that shows how to create various types of notifications. Here is an edited snippet from that sample that shows how to create a notification with an embedded action:
NotificationCompat.Action action = new NotificationCompat.Action.Builder(
R.drawable.ic_result_open, null, NotificationUtil.getExamplePendingIntent(
context, R.string.example_content_action_clicked)).build();
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("Context Text")
.addAction(action)
.extend(new NotificationCompat.WearableExtender()
.setContentAction(0));
The video walks you though a few steps that are needed, but the main thing (and what you're asking for) is the Wearable Data Layer API. The first view (the card) is a notification, but that notification launches an Activity running on the wear device. That Activity is what displays the button and sends (through the Data Layer API) a message to the camera to take the picture.
I'm trying to put my notification on top of notification area.
A solution is to set the parameter "when" to my notification object with a future time like:
notification.when = System.currentTimeMills()*2;
The code that I'm using in this:
long timeNotification = System.currentTimeMillis()*2;
Notification notification = new Notification(statusIcon,c.getResources().getString(R.string.app_name),timeNotification);
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notification.when = timeNotification;
notification.priority = Notification.PRIORITY_MAX;
but some apps (like Facebook) are able to put a simple notification with their current time over mine.
If I refresh my notification it remains under these ones.
What parameters I have to set to put my Notification to the top of the notifications area?
You should do this. Other answers seem outdated.
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.some_small_icon)
.setContentTitle("Title")
.setContentText("This is a test notification with MAX priority")
.setPriority(Notification.PRIORITY_MAX);
setPriority(Notification.PRIORITY_MAX) is important. It can also be replaced with any of the following as per requirement.
Different Priority Levels Info:
PRIORITY_MAX --
Use for critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.
PRIORITY_HIGH --
Use primarily for important communication, such as message or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.
PRIORITY_DEFAULT --
Use for all notifications that don't fall into any of the other priorities described here.
PRIORITY_LOW --
Use for notifications that you want the user to be informed about, but that are less urgent. Low-priority notifications tend to show up at the bottom of the list, which makes them a good choice for things like public or undirected social updates: The user has asked to be notified about them, but these notifications should never take precedence over urgent or direct communication.
PRIORITY_MIN --
Use for contextual or background information such as weather information or contextual location information. Minimum-priority notifications do not appear in the status bar. The user discovers them on expanding the notification shade.
For more details check the following link:
http://developer.android.com/design/patterns/notifications.html#correctly_set_and_manage_notification_priority
You can make your notification Ongoing, when it will appear higher then other usual notification. But in this case user would not be able to clear it manually.
In order to do this set flags to your Notification object:
notif.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR
Try setting priority of the notification to high
documentation > Notification Priority
Also check this question may it could help you Pin Notification to top of notification area
Please note that if you want a "heads-up" notification i.e., one that displays over the top of the current user window you must have the following set in your builder:
setDefaults(NotificationCompat.DEFAULT_VIBRATE)
The reference is in the javadoc:
A notification that vibrates is more likely to be presented as a heads-up notification, on some platforms.
Complete example for a heads-up notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.some_small_icon)
.setContentTitle("Title")
.setContentText("This is a test notification with MAX priority")
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE);