How do I display hidden notifications? - android

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

Related

Android 13: move my foreground notification back up?

I have an app which has a foreground task, and posts an ongoing notification.
Earlier until version 12, it was displayed at the topmost place on the notification drawer.
Android 13 changes this, making it appear down below:
As you can see, messenger is preceding my application.
Can I somehow post the ongoing notification to appear at the top?
I'm using it a lot so would be much more comfortable if I can have it on top (where now the messenger is).
Notification is created with basic builder:
Notification.Builder b;
Notification notification = b.setTicker(ticker)
.setSmallIcon(smallicon)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(contentIntent)
.setWhen(0)
.setAutoCancel(false)
.setOngoing(true)
.build();
Can I somehow force it to the first place?
You can try to set priority height in your code. And also avoid setting messenger app superposition on other apps. I think it will help.

How to remove an ongoing notification programmatically?

I want to remove the ongoing notification from statusbar.For example whatsapp web notification when whatsapp web is active,usb debugging notification when device is connected to pc which are not removed when swipe.Is it possible or not?If its possible then please help me.Thank u.....
Yes, you can dismiss a notification from statusbar. Just call cancel() for your notification ID.
Check out this link to learn more about removing Notifications: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Removing
If you are hiding only the notification icon on the notification bar (glance view without pulling down the slide bar to see the whole on going notifications), and its notif view still in the on going list, you can pay attention to the method setPriority of NotificationCompat.Builder.
In my case,the codeblock below saved my day!
NotificationCompat.Builder mBuilderOnGoing = new NotificationCompat.Builder(mContext);
if (Build.VERSION.SDK_INT >=16) {
mBuilderOnGoing.setPriority(NotificationCompat.PRIORITY_MIN);
}
Hope it helps

Android bundled notifications behaviour

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.

How to show Android Notifications on screen as well as status bar icon?

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!

Android: How can I put my notification on top of notification area?

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);

Categories

Resources