How to make notification stay on top? - android

I have a notification which I always want to stay on top.
I have already given it a priority of 2
Mostly it stays on top but, suppose there is a open WiFi network, then my notification goes down and the notification of open WiFi network comes on the top position.
But when I looked at the notification of vlc(for Android) paying a song, the notification of vlc stay on top and the notification of open WiFi network goes down.
I notification is ongoing and with priority 2.
What should I do. I am a beginner so please bear with me.
And thanks in advance.

have you tried this
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notify_icon)
.setContentTitle("Notification")
.setContentText("ON Top")
.setPriority(Notification.PRIORITY_MAX);
Priority Levels --> PRIORITY_MAX / PRIORITY_HIGH /PRIORITY_DEFAULT /PRIORITY_LOW /PRIORITY_MIN
read
https://material.google.com/patterns/notifications.html#correctly_set_and_manage_notification_priority
https://developer.android.com/reference/android/app/Notification.html#priority
and as StenSoft said
Android: How to create an "Ongoing" notification?

I think you can refer to this code:
Intent push = new Intent();
push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
push.setClass(this, NotificationDemo.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, push, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification updateNotification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_face_black_24dp)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
.setContentTitle("Priority Max Notification Title")
.setContentText("Priority Max Notification Message")
.setAutoCancel(false)
.setDefaults(Notification.DEFAULT_SOUND)
.setPriority(Notification.PRIORITY_MAX)
.setFullScreenIntent(pi,true)
.build();
nm.notify("priorityMaxTest",2,updateNotification);

Related

How to make a notification in Android that does not collapse itself?

The notification is now shown and hidden in the status bar after 2 seconds. How do I make the notification not go to the status bar?
val builder = NotificationCompat.Builder(context, "Over limit channel")
.setContentTitle("")
.setContentText("")
.setGroupSummary(false)
.setSmallIcon(R.mipmap.launcher_icon)
.setContent(remoteViews)
.setAutoCancel(false)
.setGroup("false")
.setGroupSummary(false)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(800, builder.build())
The trick I found is to use the method setFullScreenIntent(android.app.PendingIntent intent,boolean highPriority) with this method the notification will never be removed from the status bar except when you do it yourself.
here is an example:
val intent = Intent()
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
val builder = NotificationCompat.Builder(context, "Over limit channel")
.setContentTitle("")
.setContentText("")
.setGroupSummary(false)
.setSmallIcon(R.mipmap.launcher_icon)
.setContent(remoteViews)
.setAutoCancel(false)
.setGroup("false")
.setGroupSummary(false)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setFullScreenIntent(pendingIntent,true)
From the android Studio documentation :
An intent to launch instead of posting the notification to the status
bar. Only for use with extremely high-priority notifications demanding
the user's immediate attention, such as an incoming phone call or
alarm clock that the user has explicitly set to a particular time. If
this facility is used for something else, please give the user an
option to turn it off and use a normal notification, as this can be
extremely disruptive. On some platforms, the system UI may choose to
display a heads-up notification, instead of launching this intent,
while the user is using the device.
Params: intent – The pending intent to launch. highPriority – Passing
true will cause this notification to be sent even if other
notifications are suppressed.
UPDATE
For some API version for this to work you must add this permission on the manifest
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"></uses-permission>
You can start a foreground service with your required notification, or you can set the notification as an onGoing event to make it non dismissible

Android removing notification data in group notification when new notification is added to the group

My group notifications are working fine, however, I noticed that after a couple of hours (When not using the device for a while) when a new notification comes, the message content disappears when the notification is expanded, that is, setStyle(new NotificationCompat.BigTextStyle().bigText()) text disappears. I know it is not a request code issue, as all request code are unique and I am able to reply to the notifications as normal.
One thing to note, after the notification disappears, the next notification that comes, if the device is active (not necessarily the app), that notification will display its normal text when expanded. This seems to occur only when the device AND/OR App is not in use for a while. Is this an android system problem?
Below is picture snap shot to describe the behavior:
First notification
Second notification comes in.
Expanding both notifications, as you can see all of the messages for both of them disappears.
Code: Note that I only send summary notification once.
// Normal notification
int uniqueRequestCode = RandomUser.getUniqueRequestCode();
PendingIntent pendingIntent = PendingIntent.getActivity(
this, uniqueRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notificatio notification = new NotificationCompat.Builder(getApplicationContext(), MESSAGE_CHANNEL_ID)
.setSmallIcon(R.drawable.ne1_white_logo_crop)
.setContentTitle(title)
.setContentText(body)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.addAction(action)
.setGroup(GROUP_KEY_MESSAGE)
//.setGroupAlertBehavior(groupAlert)
.setColor(Color.BLACK)
.setAutoCancel(true)
.build();
// Summary notification
uniqueRequestCode = RandomUser.getUniqueRequestCode();
PendingIntent pendingIntent = PendingIntent.getActivity(
this, uniqueRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification summaryNotification = new NotificationCompat.Builder(this, MESSAGE_CHANNEL_ID)
.setSmallIcon(R.drawable.ne1_white_logo_crop)
// Specify which group this notification belongs to
.setGroup(GROUP_KEY_MESSAGE)
// Set this notification as the summary for the group
.setGroupSummary(true)
.setContentIntent(pendingIntent)
.build();
According to this stack overflow answer, Android by default only allows one notification to be expanded.
What I have been using now which is sufficient, and preserves expanding notification is .setStyle(new Notification.MessagingStyle.... I highly advise using MessagingStyle for situations where you want multiple notifications in a group to be expanded. As for BigTextStyle().bigText(), I am not sure of the proper procedure, so I have abandoned it.

Notification using setFullScreenIntent() for BigTextStyle opening Activity automatically

I have a very strange issue, I am working on Push Notification and it was successfully implemented but when i have used BigTextStyle in Notification to show a long message in notification area with setFullScreenIntent() method then the issue coming up the Notification opening the Activity automatically which is set in PendingIntent.
If I don't use setFullScreenIntent() then notification won't opening Activity automatically the user has to tap or click on Notification to open the Activity set in PendingIntent.
So there are two codes
Without setFullScreenIntent() working fine and not opening Activity automatically:
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
With setFullScreenIntent() also working fine but opening Activity automatically:-
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setFullScreenIntent(resultPendingIntent, true) //Whether true or false same result
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
public NotificationCompat.Builder setFullScreenIntent (PendingIntent intent, boolean highPriority)
An intent to launch instead of posting the notification to the status
bar. Only for use with extremely high-priority notifications demanding
the user's immediate attention, such as an incoming phone call or
alarm clock that the user has explicitly set to a particular time. If
this facility is used for something else, please give the user an
option to turn it off and use a normal notification, as this can be
extremely disruptive.
On some platforms, the system UI may choose to display a heads-up
notification, instead of launching this intent, while the user is
using the device.
Parameters
intent: The pending intent to launch.
highPriority: Passing
true will cause this notification to be sent even if other
notifications are suppressed.
Found here. As you can see it immediately launches the intent. I don't really know in what case you wanted to use setFullScreenIntent()?
A notification won't automatically expand when a static notification is displayed on top (could be custom bar with wifi, bluetooth and sound control)
pass setFullScreenIntent and setContentIntent with different pending intents.
Worked for me. click on Notif will work and autolaunch will stop

Android Wear Notification is not displayed if FLAG_NO_CLEAR is used

If flag "FLAG_NO_CLEAR" is used the notification gets not displayed on the Android Wear.
Does anyone know why or any workaround? I didn't find any information in the documentation.
I need the flag "FLAG_NO_CLEAR" on my notifications and have Action button for "dismiss", "snooze" etc.!
Notification flag FLAG_NO_CLEAR basically makes your notification "ongoing". Ongoing notifications posted from phone will NOT be displayed on the wearable device.
You have two solutions to your problem - both of them have advantages and disadvantages. Please read text below and decide which solution will solve your situation better:)
Solution 1 - use group:
You can make use of group feature of Android Wear framework. It's basically created to post many (grouped) notifications on wearable device and one summary notification on phone. But using this mechanism you can also post one ongoing notification on your phone and second notification only on wear. You will end up with one ongoing notification on your phone and one normal notification on your wearable device.
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// This notification will be shown only on phone
final NotificationCompat.Builder phoneNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title phone")
.setContentText("Text phone")
.setOngoing(true)
.setOnlyAlertOnce(true)
.setGroup("GROUP")
.setGroupSummary(true);
// This notification will be shown only on watch
final NotificationCompat.Builder wearableNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title wearable")
.setContentText("Text wearable")
.setOngoing(false)
.setOnlyAlertOnce(true)
.setGroup("GROUP")
.setGroupSummary(false);
notificationManager.notify(0, phoneNotificationBuilder.build());
notificationManager.notify(1, wearableNotificationBuilder.build());
This method will allow you to post an "alternative" notification for watch only, keeping your ongoing notification on phone. But (as mentioned earlier) the notification on watch cannot be ongoing - it has to be a normal notification. If you really want to have a true ongoing notification on watch you'll need to go for second solution.
Please read more about grouping (stacking) notifications here:
https://developer.android.com/training/wearables/notifications/stacks.html
Solution 2 - create wearable app:
Ongoing notifications from phone won't be shown on watch, but you can create wearable part of your Android application and post your notification directly on Android Wear. You can easily post an ongoing notification from there, but it won't be the same notification as is on phone. You will need to sync them between both devices.
Please read more about DataApi here:
https://developer.android.com/training/wearables/data-layer/index.html
https://developer.android.com/training/wearables/data-layer/data-items.html
You can also take a look at my post where I've posted a code demonstrating how to use a DataApi in practise:
https://stackoverflow.com/a/24896043/3827276
Honestly something really weird changed in wear 1.5
so a solution would be to set an alarm
Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent displayIntent = new Intent(this, WearNotif.class);
//displayIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent displayPendingIntent = PendingIntent.getActivity(this,
0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("WeirdShit")
.setContentText("some random crap")
.extend(new Notification.WearableExtender()
.setDisplayIntent(displayPendingIntent))
.setSound(alarmSound)
.build();
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(25454, notification);

Android Notification not showing in LG and some other devices

I have used the following code to display a notification while starting service in the foreground.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.statusbar_icon)
.setContentTitle("App is running")
.setStyle(new NotificationCompat.BigTextStyle())
.setPriority(Integer.MAX_VALUE)
.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setContentIntent(contentIntent);
startForeground(R.string.service_running, mBuilder.build());
The notification is showing in the notification bar of all devices except LG-E450 and a Samsung device. Not only this one, no notification from my application is not showing in the bar.
The documentation states that detail text is required and can be set via NotificationCompat.Builder#setContentText(CharSequence) (although my GS3 will display a notification without detail text)
In the App settings 'Show Notifications' has beeen turned off so it should be trun on

Categories

Resources