I'm using local notification like clock alarm with buttons to control it. My problem is that the Notification view isn't wrapping my content. So after searching around I found out that there is a workaround to achieve this by setting the view after build.
Something like this:
Notification notification = mBuilder.build();
// add remote view after build for getting bigger notification size
notification.bigContentView = remoteViews;
This work unless the notification is not first and when it is not the top notification the buttons is not shown.
How can I make the notification wrap_content even when it is not the top on the list of notifications?
Set the priority Max for notification for showing big content view like below
new Notification.Builder(this)
.setContentTitle("Media Player")
.setContentIntent(mPendingIntentHomeSong)
.setSmallIcon(R.drawable.app_icon)
.setPriority(Notification.PRIORITY_MAX)
.setContent(mNotificationContentView)
.setStyle(new Notification.BigPictureStyle()).build();
Related
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.
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.
Our custom notification only shows title (or content text if title is missing) when it's peeking. Trying to set the icon on the root notification has no effect. If I set the icon on the extender notification it shows, but the peeking notification is so small (obviously, its height is fitted to the height of the one liner title/description) that the icon wont fit. In comparison to e.g. an email notification the height of the peeking notification is much smaller.
Here's the outline of the code I'm trying to run:
Intent notificationIntent = new Intent(this, CustomNotification.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setContentTitle("Test")
.setSmallIcon(R.mipmap.ic_launcher)
.extend(new Notification.WearableExtender()
.setDisplayIntent(notificationPendingIntent)
.setCustomSizePreset(Notification.WearableExtender.SIZE_FULL_SCREEN)
)
.build();
You may be running into that fact that you have very little control over how the notification displays while in peek-view:
"Note: When the notification is peeking on the homescreen, the system displays it with a standard template that it generates from the notification's semantic data. This template works well on all watchfaces. When users swipe the notification up, they'll then see the custom activity for the notification."
https://developer.android.com/training/wearables/apps/layouts.html#UiLibrary
I tried in many ways to get the long thext styled notification but just cant reach my goal, pls someone help.
I would like to use bigTextStyle in order to get multiline message in my notifs. Here is my code:
mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, MainActivity.class), 0);
Notification noti = new Notification.Builder(ctx)
.setContentTitle("Title")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new Notification.BigTextStyle().bigText(ctx.getResources().getString(R.string.loremIpsum)))
.setContentText(ctx.getResources().getString(R.string.loremIpsum))
.setContentIntent(contentIntent).build();
mNotificationManager.notify(NOTIFICATION_ID, noti);
I use Android 4.1.2 so the op version is ok. Every notificaion is a simple one-lined message, and i dont see the whole text. Please help me.
E D I T:
Okay i got the long text, but my notif not expanding automatically, i have to swipe down with 2 fingers. How could i expand it by code..!?
You have to expand the notification to see the big text - by default only the top notification auto-expands, as described in the Notifications guide.
Maybe your notification is getting collapsed because ongoing notifications are eating up the screen space. See my answer here: Android Jelly Bean strange behavior of notifications.
even if u set the MAX priority to push your notification to the top
or it was the only notification in the notification drawer
it wont expand if there is no enough space
e.g.
to many "Ongoing-notifications" in the "Ongoing"Drawer or your screen is small
and even if it was expanded it wont show more than 8 lines , so not all the notification will shows up
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);