How to pin Notification to top of notification area - android

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

Related

How do i get my "onGoing" Notification to appear at the bottom of the drawer?

I have a long-running status that i illustrate with an ongoing notification. This works fine insofar as it can't be swiped away etc.
However, other ongoing notifications such as the "Cable charging" and "Transferring media" appear in the bottom, below all the "normal" ones.
I would like mine to appear there too, so that it does not distract from the regular application notifications, but i can't seem to find a way to do so. I have tried playing around with the Priority, but that does nothing.
Is there any way to specify the Notification in such a way?
Use FLAG_NO_CLEAR
Notification notification = notificationBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
And you will need to take care to cancel it by its ID sometime later.

Notification being cancelled by another one in Android Lollipop

I create a notification in Service (launched from the BroadcastReceiver) this way:
this.notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("Foo")
.setContentText("Bar")
.setSmallIcon(R.drawable.logo)
.setContentIntent(pIntent)
.build();
this.initializeNotificationSound(volumne, Uri.parse(melody));
this.initializeNotificationVibration();
notification.flags = Notification.FLAG_INSISTENT;
this.notificationManager.cancelAll();
this.notificationManager.notify(0, notification);
This notification is set when a particular SMS is received. I want to create a notification which will play a defined melody and will not be interrupted by the standard SMS notification.
This functionality works pretty good, however in lollipop, following scenario happens:
SMS is received
My notification is launched
After some time, notification sound is stopped and default SMS ringtone is played
This happens only when ringtone mode is silent. Otherwise, it works as expected, because of the cancelAll() method, which interrupts the SMS notification.
Thanks in advance for your response.
You can not change that. If you notice, when more than one notifications are created at the same time, even from a sole application(e.g. Facebook), the sound they make is played simultaneously. This has to do with the notification management of android system itself. Maybe the problem you are facing happens due to some code changes made to the specific Android system version (API level), maybe to the cancelAll() method. One solution would be to find the appropriate line of code for checking current system version. For example:
if(System_version <= lollipop) {
//code works fine. Keep as it is
}
else{
//find the appropriate method and call it here.
}

Heads-up Notification - Android Lollipop

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

Android prevent multiple updates of the same Notification

I am polling from a server every x minutes to get events a user is invited to. Let's say the user has a new invite and gets a notification at 10:00 am. The user never clicks on the notification, and I want to prevent the user from receiving another notification x minutes later with the same information. Basically I want to allow unique notifications to only display once and alert the user once.
You would think this line of code would work do what I want desired, but it does not.
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
Anyone have any ideas?
I'm not certain if your problem is that you are seeing multiple notifications or just multiple sounds/vibrations are happening. Two things you need to do:
Yes use the Notification.FLAG_ONLY_ALERT_ONCE or NotificationCompat.Builder#setOnlyAlertOnce(), this will prevent sounds and vibrations from occurring when you update the notification
To prevent multiple notifications from appearing in the notification area you need to make sure that when you call NotificationManager#notify(id, builder.build()); the id is a consistent value that is always the same for that particular event.

How to collapse Android notifications?

I'm sending a C2DM update to my Android app every 1/2 hour, which creates a Notification. Problem is, when I wake up in the morning I get 15 Notifications queued up in the status bar.
How do I only keep the latest notification, overwriting previous ones?
I tried looking at the C2DM documentation (http://code.google.com/android/c2dm/) which mentions a parameter called collapse_key, but I couldn't find an explanation for how to use it, nor am I sure the solution lies on the C2DM side.
Thanks!
If you want to cancel any previous notifications that has been set on the view you can try setting one of these flags.
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_UPDATE_CURRENT
Something like this should replace your old notification i believe
NotificationManager mManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this,test.class);
Notification notification = new Notification(R.drawable.icon, "Notify", System.currentTimeMillis());
notification.setLatestEventInfo(this,"App Name","Description of the notification",
PendingIntent.getActivity(this.getBaseContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
mManager.notify(0, notification);
Notification has a property called number that shows a little number below the icon (for multiple notification). It lets you use the same Icon for Multiple Notification.
Use the same ID while updating your notification. :) Cheers.
In addition to the other answers, there is a parameter in your C2DM request that is called delay_while_idle. Make sure you are NOT including that or make it false. Your phone is "idle" when the screen is off (ie while you are sleeping). Google queues up all your messages on the server until the phone is not idle (ie when you turn on the screen in the morning). Then, Google sends all 15 messages at once and you display them at that time.
In the chrome to phone source, there is a method called sendNoRetry with this line:
if (delayWhileIdle) {
postDataBuilder.append("&")
.append(PARAM_DELAY_WHILE_IDLE).append("=1");
}
Make sure it is not true, then Google servers will send you your C2DM message every 30 minutes as expected.
collapse_id key should do the job. For updating any previous notification, just use the same key. To generate a new notification on device, use a different key.
For example,
* for chat notifications use the key "chat" (collapse_id = "chat")
* for invitations use the key "invite" (collapse_id = "invite")
So all the unqiue collapse_id notifications will group on device.
For more details visit: https://documentation.onesignal.com/reference#create-notification

Categories

Resources