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
Related
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.
My test phone is a Samsung Galaxy S6 using Android 7.0 Nougat.
In other apps, Android push notifications stay in the status bar, but in my app the push notification disappears after the notification is received.
Problem is DISAPPEAR NOTIFICATION FROM STATUS BAR.
Not about NOTIFICATION ALARM.
Push notification is well done working.
After ringing, there is no push in status bar.
Does anyone know about this situation?
Here is the code:
NotificationCompat.Builder builder = new
NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.icon_noti)
.setContentTitle('My App')
.setContentText('test')
.setDefaults(Notification.DEFAULT_SOUND)
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,
0, new Intent(getApplicationContext(), MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
If someone knows about this issue, please answer.
Thanks for reading.
For me it works perfectly (except for the single quotes here .setContentTitle('My App') and here .setContentText('test'))!
Maybe there are some system issues - or maybe this behavior is caused by some other part of your app.
By the way, note that NotificationCompat.Builder is deprecated; now it requires an additional String channelId.
I had this issue and the problem was that I had two different notifications with the same id.
One was tied to a service so when I unbound the service it killed the notification but since both had the same ID it killed both notifications.
Changing the notification id when calling the notify method fixed the issue:
notificationManager?.Notify(2, notification); //Previously I had "1" which matched another notification that had 1 as ID
Took me like 4 hours to find out but at the end in my scenario it had nothing to do with the notification settings.
I am trying to create a simple notification. With this code, the notification appears as an icon in the notification area, and when I open up the notification drawer it is there...
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.midday);
mBuilder.setContentTitle("Midday");
mBuilder.setContentText("Now it is midday");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
I want this notification content to actually show on the top of the screen, not just that the little icon appear on the top bar.
Like this,
I tried searching for this and found that...
mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
should help. But it didn't show the notification, only the little icon appears.
If you can help, that would be great.
This is the answer:
Its called a Heads-up Notifications.
This should be enough:
mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
However, I have to add this line:
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
Hopefully google will fix this
I feel like this should be trivial but I can't seem to make a notification show up on the phone's screen - it only shows up in the status bar at the top.
For an example of what I want to do, here's how Facebook Messenger shows up on the screen when you receive a message.
Whenever I send a notification, all it does is show the little icon in the status bar - even if I set the priority to PRIORITY_MAX. Is there another setting I need to do to make it show on screen instead of just status bar?
The Notification display code:
PendingIntent contentIntent = PendingIntent.getActivity(context, nextId++, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(description)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_stat_notification)
.setLargeIcon(largeIcon)
.setPriority(Notification.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL);
if (android.os.Build.VERSION.SDK_INT >= 21) {
builder.setColor(context.getResources().getColor(R.color.orange_500))
.setVisibility(Notification.VISIBILITY_PUBLIC);
}
Notification notification = builder.build();
notificationManager.notify(id, notification);
All things considered, it's a really good idea to use NotificationCompat.Builder over Notification.Builder, let alone creating a Notification manually. It gives you nice backwards compatibility with graceful degradation (all the way back to API Level 4, otherwise known as "gadzooks, that's old"). AFAIK, it's the only way to get some of the Android Wear stuff going, when used in concert with NotificationManagerCompat. And, in this case, it seems to be happier with the newer Android 5.0+ features.
In this case, setPriority(NotificationCompat.PRIORITY_HIGH) on a NotificationCompat.Builder, used with NotificationManagerCompat, will give you the heads-up notification on Android 5.0+.
Another point, make sure the 'importance' of the notification channel you have set up for your notification is set to NotificationManager.IMPORTANCE_HIGH.
This configures how visually intrusive notifications posted to this channel are and 'high' will allow it to peek. If you have this set to NotificationManager.IMPORTANCE_DEFAULT then it won't!
Bear in mind when configuring a notification channel, once the code has ran on your device, you won't be able to alter this again. So if you need to change the importance you will need to uninstall the app and then re-run and you should see the notification on your screen!
Is it possible to have a notification that shows a different text (content title and content text) in the Android wear device and in the mobile device?
Not at the moment. However, you can achieve this effect in the following way:
post a notification on the phone with setLocalOnly(true)
post a DataItem using a DataAPI that describes the notification and changed text
when the wearable receives the DataItem, post the notification with different text, again setting setLocalOnly(true)
on each notification alse call setDeleteIntent so you know, when there are dismissed
when on of the notifications gets dismissed, delte the DataItem from point 2.
when the DataItem gets deleted, you will receive a callback; delete the remaining notification
There might be some corner cases here I don't see immediately, but the general approach should allow you to achieve what you want.
Yes, it's possible now with little tricky and bug on Android wear which help's us. More Over this trick doesn't need Android wear API, just a normal Notification with RemoteViews is enough.
NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentText("This msg won't display in your phone, only on wear you can see.")
.setContentTitle("Hello")
.setContentIntent(
PendingIntent.getActivity(context, 10,intent,PendingIntent.FLAG_ONE_SHOT));
NotificationManager mNM = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
RemoteViews contentView = new RemoteViews(context.getPackageName(),
R.layout.notification_layout);
contentView.setTextViewText(R.id.noti_text,"This message won't display in your wear device, only on phone you can see.");
contentView.setImageViewResource(R.id.noti_image,R.drawable.ic_launcher);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(50, notification);
Now run the app on your device and check the notification on both watch and phone, the content inside the RemoteViews won't display in your watch but on phone it will display and if you remove the .setContentTitle() & .setContentText(), then the RemoteViews content will be displayed on both watch and phone too.
Actually you can achieve this using
.setGroup(GROUP_KEY)
.setGroupSummary(true)
On your phone notification. Then create a notification with the data that you want to show on the watch and set
.setGroup(GROUP_KEY)
to the same group that your phone notification. This is used to show stack notifications but if you use it with only one then it does the trick.
Complete documentation: Stacking Notifications