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.
Related
I've been trying for a while to replicate some of the demo notifications from the Android Wear app with custom layouts, but there is one thing I can't get working: showing the custom layout when the notification is minimized. I'm using the setDisplayIntent() method to embed an activity into the notification with custom a layout.
The thing is, when the notification is expanded the activity's layout is displayed properly. But then the notification minimizes the activity is hidden and the default notification data is displayed (contentTitle, contentText).
I'm displaying the notification with setDisplayIntent() from the wear application as shown here:
Intent page1Intent = new Intent(context, Page1Activity.class);
Notification page1notif = new Notification.Builder(context)
.setContentTitle("Notification title")
.setSmallIcon(R.drawable.logo_renfe)
.extend(new Notification.WearableExtender()
.setBackground(BitmapFactory.decodeResource(context.getResources(), R.drawable.train_background))
.setDisplayIntent(PendingIntent.getActivity(context, 0, page1Intent, PendingIntent.FLAG_UPDATE_CURRENT))
.setCustomSizePreset(Notification.WearableExtender.SIZE_LARGE))
.build();
Here's what I'm trying to achieve:
And here's what I've got:
What kind of sorcery is Google doing to show their demo cards? Is there something from the API that I've missed or are they using a different technique?
Thanks!
I am developing a simple app that starts a service when a button is tapped... The service create an ongoing notification but I don't want it to display any icon in the status bar...
In this picture you can see WiFi ADB has a standard ongoing notification that cannot be dismissed...
Google Now (The 62 Cloudy), Automatic, and Automate do not display any icon when the notification bar is closed and they are in a separate group (under that grey line).
I looked everywhere for how to achieve that but can't find anything... Even the Android documentation which is usually quite exhaustive doesn't provide any information about it.
EDIT:
Right now this is how I display my notification:
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Power Napp")
.setContentText("Napping...")
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
There's a simple trick. Add:
.setPriority(Notification.PRIORITY_MIN)
And the notification won't show the icon in the status bar.
http://developer.android.com/reference/android/app/Notification.html#PRIORITY_MIN
isn't very clear about this but it's documented here:
http://developer.android.com/design/patterns/notifications.html#correctly_set_and_manage_notification_priority
I feel like this should be trivial but I can't seem to make a notification show up on the phone's screen - it only shows up in the status bar at the top.
For an example of what I want to do, here's how Facebook Messenger shows up on the screen when you receive a message.
Whenever I send a notification, all it does is show the little icon in the status bar - even if I set the priority to PRIORITY_MAX. Is there another setting I need to do to make it show on screen instead of just status bar?
The Notification display code:
PendingIntent contentIntent = PendingIntent.getActivity(context, nextId++, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(description)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_stat_notification)
.setLargeIcon(largeIcon)
.setPriority(Notification.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL);
if (android.os.Build.VERSION.SDK_INT >= 21) {
builder.setColor(context.getResources().getColor(R.color.orange_500))
.setVisibility(Notification.VISIBILITY_PUBLIC);
}
Notification notification = builder.build();
notificationManager.notify(id, notification);
All things considered, it's a really good idea to use NotificationCompat.Builder over Notification.Builder, let alone creating a Notification manually. It gives you nice backwards compatibility with graceful degradation (all the way back to API Level 4, otherwise known as "gadzooks, that's old"). AFAIK, it's the only way to get some of the Android Wear stuff going, when used in concert with NotificationManagerCompat. And, in this case, it seems to be happier with the newer Android 5.0+ features.
In this case, setPriority(NotificationCompat.PRIORITY_HIGH) on a NotificationCompat.Builder, used with NotificationManagerCompat, will give you the heads-up notification on Android 5.0+.
Another point, make sure the 'importance' of the notification channel you have set up for your notification is set to NotificationManager.IMPORTANCE_HIGH.
This configures how visually intrusive notifications posted to this channel are and 'high' will allow it to peek. If you have this set to NotificationManager.IMPORTANCE_DEFAULT then it won't!
Bear in mind when configuring a notification channel, once the code has ran on your device, you won't be able to alter this again. So if you need to change the importance you will need to uninstall the app and then re-run and you should see the notification on your screen!
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 have a notification with a addidional action so user is able to go to the main activity by clicking the main notification or stop the service clicking the secondary (extented) notification. This works fine for post JellyBean Androids (4.1+). How to achieve similar functionality in older systems (API 11+)?
noti = new NotificationCompat.Builder(this)
.setContentTitle("service")
.setContentText("running")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendIntent)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(R.drawable.ic_launcher, "Stop", pIntent)
.build();
startForeground(12345, noti);
From the docs:
Builder class for NotificationCompat objects. Allows easier control
over all the flags, as well as help constructing the typical
notification layouts.
On platform versions that don't offer expanded notifications, methods
that depend on expanded notifications have no effect.
For example, action buttons won't appear on platforms prior to Android
4.1. Action buttons depend on expanded notifications, which are only available in Android 4.1 and later.
For this reason, you should always ensure that UI controls in a
notification are also available in an Activity in your app, and you
should always start that Activity when users click the notification.
To do this, use the setContentIntent() method.
and from the addAction():
Add an action to this notification. Actions are typically displayed by
the system as a button adjacent to the notification content. Action
buttons won't appear on platforms prior to Android 4.1. Action buttons
depend on expanded notifications, which are only available in Android
4.1 and later. To ensure that an action button's functionality is always available, first implement the functionality in the Activity
that starts when a user clicks the notification (see
setContentIntent()), and then enhance the notification by implementing
the same functionality with addAction().
I found this tutorial that you might find useful.