I'm trying to show a notification-type heads-up but I could not. What I tried
final Notification.Builder notif = new Builder(getApplicationContext())
.setContentTitle(getString(R.string.title))
.setContentText(getString(R.string.text))
// .setTicker(getString(R.string.tick)) removed, seems to not show at all
// .setWhen(System.currentTimeMillis()) removed, match default
// .setContentIntent(contentIntent) removed, I don't neet it
.setColor(Color.parseColor(getString(R.color.yellow))) //ok
.setSmallIcon(R.drawable.ic_small) //ok
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
// .setCategory(Notification.CATEGORY_CALL) does not seem to make a difference
.setPriority(Notification.PRIORITY_MAX); //does not seem to make a difference
// .setVisibility(Notification.VISIBILITY_PRIVATE); //does not seem to make a difference
mNotificationManager.notify(Constants.NOTIFICATION_ID, notif.build());
The notification is shown only as an icon in the bar.
I'm using API 21 on API 21 emulator (not L preview)
I have tried:
android:Theme.Holo.NoActionBar,
android:Theme.Holo.NoActionBar.Fullscreen
and NotificationCompat.Builder
SDK examples are not available. does anyone know how to do it?
I made it working by adding:
.setDefaults(Notification.DEFAULT_VIBRATE)
is this the best way?
According to Notifications, you are required to set a vibrate or ringtone to make Heads-up work. However, here's a quick hack that doesn't require VIBRATE permission to produce a head-up notification:
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);
EDIT:
Don't abuse heads-up notification. See here for when to use heads-up notification:
MAX: 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.
HIGH: Primarily for important communication, such as messages or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.
According to Google:
https://developer.android.com/design/patterns/notifications.html
If a notification's priority is flagged as High, Max, or full-screen, it gets a heads-up notification.
So the following code should generate an heads-up notification:
.setPriority(Notification.PRIORITY_MAX)
Should be enough. But apparently the .setDefaults(Notification.DEFAULT_VIBRATE) has to be set also. Hopefully Google will fix this in their final release of Android 5.0.
Not sure if bug or feature...
All my apps doesn´t show the Notification, for example i have a Nexus 6 with Android 5.1.1, but i think this is an issuse since Android 5.0, i had to set:
.setPriority(Notification.PRIORITY_HIGH)
Correctly set and manage notification priority
Android supports a priority flag for notifications. This flag allows you to influence where your notification appears, relative to other notifications, and helps ensure that users always see their most important notifications first. You can choose from the following priority levels when posting a notification:
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.
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.
DEFAULT Use for all notifications that don't fall into any of the other priorities described here and if the application does not
prioritize its own notifications
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.
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.
To set the priority, use the setPriority function (introduced in API 16) alongwith setDefaults (added in API 11) of Notification Builder. Choose the priority DEFAULT, HIGH, LOW, MAX, MIN as per the requirement of your app. Defaults can also be chosen here.
A small snippet:
notification = NotificationBuilder(service)
notification.setPriority(Notification.PRIORITY_MAX)
notification.setDefaults(Notification.DEFAULT_ALL)
Please check that your phone is not in “silent” or “do not disturb” mode. I spent day before I found it. I just leave this comment for those who get the same problem and found this question.
Should set high priority and use ringtones or vibrations.
notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
Ref: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up
Heads-up Notifications
With Android 5.0 (API level 21), notifications can appear in a small
floating window (also called a heads-up notification) when the device
is active (that is, the device is unlocked and its screen is on).
These notifications appear similar to the compact form of your
notification, except that the heads-up notification also shows action
buttons. Users can act on, or dismiss, a heads-up notification without
leaving the current app.
Examples of conditions that may trigger heads-up notifications
include:
The user's activity is in fullscreen mode (the app uses fullScreenIntent), or
The notification has high priority and uses ringtones or vibrations
For devices running Android 8.0 (API level 26) and higher the notification channel requires high importance
new NotificationChannel("ID", "Channel Name", NotificationManager.IMPORTANCE_HIGH);
Add this line in your code to display heads up notification it's only working for Lollipop version
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
You don't need to set vibrate. You only need to set sound. It's less intrusive. I don't get any sound on mine, but the notification displays on top. Make sure you use PRIORITY_HIGH and DEFAULT_SOUND.
NotificationChannel channel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel = new NotificationChannel("my_channel_01",
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
}
Notification notification =
new NotificationCompat.Builder(this, "notify_001")
.setSmallIcon(R.drawable.ic_check)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setChannelId("my_channel_01").build();
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notification);
Related
I have a foreground service in my app, whose persistent notification has a timer in it, which means that the notification is sent once per second to update the timer that is shown in the notification. This means that on some devices, where notifications are set to either wake the screen entirely or show a dark version on the screen briefly, the screen is constantly awake.
Is there a way to send the notification in a way that it won't wake up the screen? Setting it as a silent notification on the device fixes this, but the point of a foreground service notification is that it's prominent on the device, so this isn't a great solution, and not all users would know to do that.
This is how I'm building the notification:
NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Currently Reading")
.setSound(null)
.setContentIntent(TaskStackBuilder.create(this).run {
addNextIntentWithParentStack(timerIntent)
getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
})
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
Have you tried updating the notification instead? And use setOnlyAlertOnce()
"You can optionally call setOnlyAlertOnce() so your notification interupts the user (with sound, vibration, or visual clues) only the first time the notification appears and not for later updates."
Check this link
https://developer.android.com/training/notify-user/build-notification.html#Updating
setPriority(NotificationCompat.PRIORITY_DEFAULT)?
lower it, so wont be shown on lockscreen (android 7 and up)
and here we go with a small fix for this part:
setPriority(NotificationCompat.PRIORITY_LOW)
you can also add:
setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
but it'll also remove notification from lockscreen...
docs:
https://developer.android.com/training/notify-user/build-notification.html#lockscreenNotification
https://developer.android.com/training/notify-user/channels#importance
hope this helps =]
We are working on an app which can recognize the name of the incoming call from the phone number. (Our problem is similar to this one but the solution is different) We have created a CallScreeningService which receives the phone number of the incoming phone call and we are able to display the toast showing the caller id whenever there is a call. However, instead of displaying a toast we want to display a notification. (We had also tried displaying a pop up window over Android native incoming call screen like true caller Android app but failed to show it when app is in background or closed state) Now we are also able to create a notification of caller ID whenever there is a call but it is not visually appearing on the screen.
Is it possible to make the notification visually appear on the screen at the same time as an incoming phone call? If yes, how?
Thank you so much!
Assuming you found a way to keep your app alive in doze mode you need to set your Notification's Channel importance or notification's priority to high or max(side note: Notification channel's importance overrides Notification builder's priority).
NotificationChannel:
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
Notification builder:
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);
If doze mode is killing your app, you can use foreground service with return Service.START_STICKY; or WakefulBroadcastReceiver mixed with startWakefulService().
It seems that lowest notification level you can set when creating a Notification Channel on Android Oreo is Medium.
This is the case even when using NotificationManager.IMPORTANCE_NONE.
NotificationChannel sendingChannel = new NotificationChannel("SENDING", "Sending", NotificationManager.IMPORTANCE_NONE);
NotificationManager.IMPORTANCE_NONE, NotificationManager.IMPORTANCE_MIN, and NotificationManager.IMPORTANCE_LOW all set the Notification Channel importance to Medium.
There does not appear to be a way to set the notification importance to low on Android Oreo.
I ran into this issue when trying to set a notification channel to Low for use with a foreground service. I can confirm in that case, the level gets increased to Medium by the system.
See this stack overflow question/answer for details: Default priority of notification channel on Android 8
Documentation for the behavior exists (again, assuming you're using a foreground service) although I would argue it's a bit unclear: https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_MIN for details
In Android, if a user sets "Show Notifications" of a specific App to off, is the notification still created? If yes, is it somehow possible to access these created but not shown notifications?
As an example, consider that to create a Status bar Notification, below code is used:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
And to POST this notification, below code is used:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
But since the "Show Notification" is OFF, above code don't show the Notification on Status bar. So as you can see, although the NotificationCompat.Builder has CREATED a Notification object/instance and filled it with other details such as Title or Text etc. you can NOT access it as it is NOT Allowed to POST by System because of user's preference settings.
Also, Since Android 4.1, users can turn off notifications of an app from application manager, but there is NO API to acheive the same from Application code (on Non-rooted device,that is). And It is not possible to disable notifications from other apps, and you can only control notifications generated by your own app.
Few more pointers that you must have in mind:
1.As a developer we have no way to know whether a call to notify was effective or not. So if I really need to check if the notifications are disabled for the current application there is NO Such setting for that in the API.
2.You really shouldn't concern yourself with it. Just assume your notification was successful. If the user has explicitly disabled your notifications, then he/she probably had good reason to do so, and your application should not care whether the notification was displayed or not
I have found one answer here:
Pin Notification to top of notification area
Does anyone known what the Vaiden's answer means?
notification.when = previousTimestamp;
How to get the "previousTimestamp"?
Thanks.
Vaiden means to make up timestamps for all of your notifications in the order you want them to appear in. Give a more recent timestamp for the ones that you want to have appear up top. The key point is that in the following example, previousTimestamp does not change, as opposed to constantly updating it with the current system time on every refresh (in the case of the question you are referring to, every 3 seconds).
set the following on all of your notifications:
myNotification.when = previousTimestamp;
not
myNotification.when = System.currentTimeMillis();
EDIT: Besides Vaiden's method, there are two things you can do to move your notifications up.
You can set the following two flags on your notification to make it ongoing, keeping it above non-ongoing notifications. Keep in mind that notifications that the user would expect to be able to clear, such as an email notification, should not use this method.
myNotification.flags |=
Notification.FLAG_ONGOING_EVENT;
myNotification.flags |= Notification.FLAG_NO_CLEAR;
You can set the notification priority to a higher level of you are developing for Android 4.1 Jelly Bean and above.
They are MAX, HIGH, DEFAULT, LOW, and MIN. If your app runs below Jelly Bean as well, then the priority is just default. See http://developer.android.com/design/patterns/notifications.html for more details on what priority is appropriate for which type of notification, but just remember, as with the ongoing flag, don't abuse the high priority setting.
NotificationCompat.setPriority(PRIORITY_MAX);