I'm generating two local notifications from my app using two different
notification channel. notification configurations will be as below,
1.) Notification1 (notification_channel: msg_1 and notification_id: 1)
2.) Notification2 (notification_channel: msg_2 and notification_id: 1)
What will happen in this case?
It will show single notification
OR
It will show two separate notification
I want to know the expected behaviour from your end?
I've explored about the scenario & it will display as diffrent notification because OS use notificationId to show notification seperately in notificaition stack on tray. If you pass same notificationId ,OS will just overlap older notification with the new one.
Also, notification channel configurations are just applied to notificationId to show specific behaviour.
Related
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.
Android N introduced Bundled notifications:
posting 4 or more notifications without a group will cause those
notifications to be automatically bundled.
I'm creating four very similar notifications with different tag and id but with the same icon. After notifying NotificationManagerCompat and creating 4rd notification, all notifications are bundled into one with my application name and grey icon. And this leads to strange behaviour because it looks like that icon is greyed out shape of my applications launcher icon instead of smallIcon from NotificationCompat.Builder
I created sample app and tried to simulate the same environment, and created 4 notifications using same builder methods as in first application. Instead grey shape, I can see original smallIcon from builder which is correct behaviour.
On the screen, first notification is from my sample app, the second one is from my main app.
Creating notifications in both apps looks the same:
NotificationCompat.Builder notifBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.alert_octagon_white)
.setContentTitle("contentTitle 2")
.setTicker("ticker 2")
.setContentText("contentText 2")
.setStyle(new NotificationCompat.BigTextStyle().bigText("bigText 2"));
NotificationManagerCompat.from(this).notify("tag2", 2, notifBuilder.build());
Where can I look for the differences? How can I set bundled notification icon?
I am not sure of the difference in behavior you are seeing but i think something in Android Nougat default Notification bundling is screwed up.
To avoid that specifically call setGroup() and setGroupSummary() on your Builder object.That would solve the issue.
I am doing Push notification message in android devices and follows GCM documentation for my reference and I have scenario that in the notification itself I have to show buttons and user clicked it will trigger the respective actions.
From the GCM documentation, in the notification payload we can add click_action to trigger the action when user touched notification...
How to show buttons (Like Accept/Reject ) in the notification message?
You can use .addAction in Notification.Builder.
Notification notification = new Notification.Builder(context)
// Show controls on lock screen even when user hides sensitive content.
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_stat_player)
// Add media control buttons that invoke intents in your media service
.addAction(R.drawable.ic_accept, "Accept", prevPendingIntent) // #0
.addAction(R.drawable.ic_reject, "Reject", pausePendingIntent) // #1
// Apply the media style template
EDIT 1
Refer this link.
There is no way to add action buttons to a notification message. This may be a feature added later on but currently it does not exist.
Notification messages allow you to create a very specific type of notification, if you want to have a very custom notification then you should use data messages (not notification messages) and then when the message is received use the Notification.Builder to generate your custom notification with as many features as are available :)
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);