How to avoid Heads-up notification on android Oreo and above? - android

I want to show the notification in the background. Means, while user opening the notification screen.
But its coming on the top of my app screen. I don't want to show at the top of the my app.
I want to show only in the notification bar.
Can someone suggest, what is the property that I have to set.

Make sure your activity is not full screen.
Set low priority to your notification
NotificationCompat.Builder mBuilder.setContentTitle(fileName)
.setContentText("Notification")
.setSmallIcon(R.drawable.icon)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
and low importance to your notification channel
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, IMPORTANCE_LOW);

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 programatically minimize a Notification on Android [duplicate]

I would like to have an ongoing notification for my ForegroundService that requires as small place as possible. I like the "Android System - USB charging this device" style, but I cannot find any example how to achieve this.
Can anyone point me in the right direction?
Update
The style is given to the notification if the channel is assigned the importance IMPORTANCE_MIN.
It looks like there is no way to use Androids built in style for notifications of IMPORTANCE_MIN to be used with a ForegroundService.
Here is the description of IMPORTANCE_MIN:
Min notification importance: only shows in the shade, below the fold. This should not be used with Service.startForeground since a foreground service is supposed to be something the user cares about so it does not make semantic sense to mark its notification as minimum importance. If you do this as of Android version Build.VERSION_CODES.O, the system will show a higher-priority notification about your app running in the background.
To display a compact single line notification like the charging notification, you have to create a Notification Channel with priority to IMPORTANCE_MIN.
#TargetApi(Build.VERSION_CODES.O)
private static void createFgServiceChannel(Context context) {
NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_MIN);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(channel);
}
And then create an ongoing notification like that:
public static Notification getServiceNotification(Context context) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "channel_id");
mBuilder.setContentTitle("One line text");
mBuilder.setSmallIcon(R.drawable.ic_notification);
mBuilder.setProgress(0, 0, true);
mBuilder.setOngoing(true);
return mBuilder.build();
}
NOTE
Please note that I've tested it with an IntentService instead of a Service, and it works. Also I've just checked setting a Thread.sleep() of 15 seconds and the notification is showing perfectly until the IntentService stops itself.
There are some images (sorry some texts are in Spanish, but I think the images are still useful):
And if you drag down and opens the notification, it's shown as follows:
EXTRA
If you notice that Android System shows a notification indicating all apps which are using battery (apps with ongoing services), you can downgrade the priority of this kind of notifications and it will appear as one line notifications like the charging notification.
Take a look at this:
Just long click on this notification, and select ALL CATEGORIES:
And set the importance to LOW:
Next time, this "battery consumption" notification will be shown as the charging notification.
You need to set the Notification priority to Min, the Notification Channel importance to Min, and disable showing the Notification Channel Badge.
Here's a sample of how I do it. I've included creating the full notification as well for reference
private static final int MYAPP_NOTIFICATION_ID= -793531;
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "myapp_ongoing";
CharSequence name = context.getString(R.string.channel_name_ongoing);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_MIN);
channel.setShowBadge(false);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_stat_notification_add_reminder)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(context.getString(R.string.create_new))
.setOngoing(true).setWhen(0)
.setChannelId(CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_MIN);
// Creates an intent for clicking on notification
Intent resultIntent = new Intent(context, MyActivity.class);
...
// The stack builder object will contain an artificial back stack
// for the
// started Activity.
// This ensures that navigating backward from the Activity leads out
// of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MyActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(MYAPP_NOTIFICATION_ID, mBuilder.build());
To answer the original question:
There seems to be no built-in way on Android O to get a single line, ongoing notification for a ForegroundService. One could try adding a custom design, but as different phones have different designs for notification, that solution is hardly a good one.
There is hope, however :)
On Android P the notification in a NotificationChannel of IMPORTANCE_LOW with a priority of PRIORITY_LOW is compacted to a single line even for a ForegroundService. Yeah!!
I made the size of foreground service notification smaller by creating an empty custom view like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
and then creating the notification like this:
RemoteViews notifiactionCollapsed = new RemoteViews(getPackageName(),R.layout.notification_collapsed);
Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID)
.setSmallIcon(R.drawable.eq_icon)
.setCustomContentView(notifiactionCollapsed)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setShowWhen(false)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setOngoing(true)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
notification);
This helps in reducing the height of the notification but still I am not sure about how to hide the notification icon.

Android O - Single line Notification - like the "Android System - USB charging this device"

I would like to have an ongoing notification for my ForegroundService that requires as small place as possible. I like the "Android System - USB charging this device" style, but I cannot find any example how to achieve this.
Can anyone point me in the right direction?
Update
The style is given to the notification if the channel is assigned the importance IMPORTANCE_MIN.
It looks like there is no way to use Androids built in style for notifications of IMPORTANCE_MIN to be used with a ForegroundService.
Here is the description of IMPORTANCE_MIN:
Min notification importance: only shows in the shade, below the fold. This should not be used with Service.startForeground since a foreground service is supposed to be something the user cares about so it does not make semantic sense to mark its notification as minimum importance. If you do this as of Android version Build.VERSION_CODES.O, the system will show a higher-priority notification about your app running in the background.
To display a compact single line notification like the charging notification, you have to create a Notification Channel with priority to IMPORTANCE_MIN.
#TargetApi(Build.VERSION_CODES.O)
private static void createFgServiceChannel(Context context) {
NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_MIN);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(channel);
}
And then create an ongoing notification like that:
public static Notification getServiceNotification(Context context) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "channel_id");
mBuilder.setContentTitle("One line text");
mBuilder.setSmallIcon(R.drawable.ic_notification);
mBuilder.setProgress(0, 0, true);
mBuilder.setOngoing(true);
return mBuilder.build();
}
NOTE
Please note that I've tested it with an IntentService instead of a Service, and it works. Also I've just checked setting a Thread.sleep() of 15 seconds and the notification is showing perfectly until the IntentService stops itself.
There are some images (sorry some texts are in Spanish, but I think the images are still useful):
And if you drag down and opens the notification, it's shown as follows:
EXTRA
If you notice that Android System shows a notification indicating all apps which are using battery (apps with ongoing services), you can downgrade the priority of this kind of notifications and it will appear as one line notifications like the charging notification.
Take a look at this:
Just long click on this notification, and select ALL CATEGORIES:
And set the importance to LOW:
Next time, this "battery consumption" notification will be shown as the charging notification.
You need to set the Notification priority to Min, the Notification Channel importance to Min, and disable showing the Notification Channel Badge.
Here's a sample of how I do it. I've included creating the full notification as well for reference
private static final int MYAPP_NOTIFICATION_ID= -793531;
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "myapp_ongoing";
CharSequence name = context.getString(R.string.channel_name_ongoing);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_MIN);
channel.setShowBadge(false);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_stat_notification_add_reminder)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(context.getString(R.string.create_new))
.setOngoing(true).setWhen(0)
.setChannelId(CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_MIN);
// Creates an intent for clicking on notification
Intent resultIntent = new Intent(context, MyActivity.class);
...
// The stack builder object will contain an artificial back stack
// for the
// started Activity.
// This ensures that navigating backward from the Activity leads out
// of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MyActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(MYAPP_NOTIFICATION_ID, mBuilder.build());
To answer the original question:
There seems to be no built-in way on Android O to get a single line, ongoing notification for a ForegroundService. One could try adding a custom design, but as different phones have different designs for notification, that solution is hardly a good one.
There is hope, however :)
On Android P the notification in a NotificationChannel of IMPORTANCE_LOW with a priority of PRIORITY_LOW is compacted to a single line even for a ForegroundService. Yeah!!
I made the size of foreground service notification smaller by creating an empty custom view like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
and then creating the notification like this:
RemoteViews notifiactionCollapsed = new RemoteViews(getPackageName(),R.layout.notification_collapsed);
Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID)
.setSmallIcon(R.drawable.eq_icon)
.setCustomContentView(notifiactionCollapsed)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setShowWhen(false)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setOngoing(true)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
notification);
This helps in reducing the height of the notification but still I am not sure about how to hide the notification icon.

Avoid notification popup peek when app is in foreground

From https://material.io/guidelines/patterns/notifications.html#notifications-behavior , I really we are able to notify user, without showing a notification peek.
I would like to show a flashing liked icon, in status bar, without pop up notification peek. (If you watch the first video under section From https://material.io/guidelines/patterns/notifications.html#notifications-behavior , you can see the flashing in status bar)
Flashing icon in status bar
However, I'm not entirely sure how to achieve that. Whenever I notify user, there will be a notification popup peek.
Notification popup peek
My code is as follow
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context.getApplicationContext(), org.yccheok.notification.Utils.createNotificationChannel())
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(contentTitle)
.setTicker(ticker)
.setColorized(true)
.setColor(context.getResources().getColor(R.color.accent_material_light))
.setContentText(contentText);
mBuilder.setSound(Uri.parse(getStockAlertSound()));
mBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
mBuilder.setAutoCancel(true);
// Need BIG view?
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
// Sets a title for the Inbox style big view
inboxStyle.setBigContentTitle(contentTitle);
inboxStyle.setSummaryText(summaryText);
for (SpannableString notificationMessage : notificationMessages) {
inboxStyle.addLine(notificationMessage);
}
mBuilder.setStyle(inboxStyle);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
I was wondering, when my app is in foreground, how can I avoid notification popup peek, but only show a flashing icon in status bar.
.setPriority(NotificationManager.IMPORTANCE_LOW)

How to control frequency of notification update on locked screen

I have an application which updates a notification through a service.
This notification is updated every second. It shows a timer.
Currently on nougat, with device locked, notification update triggers screen to wake up, to show the updated notification.
I would like to control this as my notification is really frequent.
I would also like to avoid changing the update frequency of the notification. Doing so shows less accurate information to the user.
So what I am looking for is a programmatic way to control wake up screen frequency upon notification updates.
thank you :)
This below notification can be used to update the user frequently and also the screen will not wake up when locked up. you may try
if(notificationBuilder == null) {
notificationBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.extend(wearableExtender)
.setAutoCancel(true)
.setContentTitle("Random Notification")
.setContentText("Appears!")
.setOnlyAlertOnce(true)
.setOngoing(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(contentIntent);
}else{
notificationBuilder.setContentText("change this to current updated text");
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MainActivity.this);
notificationManager.notify(888, notificationBuilder.build());

Categories

Resources