I have a notification that contains a small amount of text but it still to big for it to be displayed in the notification bar so it is abbreviated. The only solution I've found it to make a custom notification but it seems a bit overkill for what I am trying to do.
Can I make the notification scrolling text or expand the notification text area so it will show the full text not just the first few words of it?
I currently call the notification with:
private void triggerNotification() {
Notification notification = new Notification(R.drawable.ic_launcher,
"Title", System.currentTimeMillis());
notification.setLatestEventInfo(this, "First text",
"This is the text that gets abberviated with .. when it is too long",
PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
PendingIntent.FLAG_CANCEL_CURRENT));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(0, notification);
}
In API 16+, there is a Notification.Builder class that allows you to add a subtext which can be of multiple lines (Like Gmail notifications). I don't know if Notification.Builder is included in the support library but I guess it might be. You can check it.
However, if it is not in the support library and you have to stick to your minSdkVersion, I'm afraid that there is no way to do what you want other than implementing your own notification version.
UPDATE:
The last revision of the support library (revision 10 which was released on August 2012) introduces the Android 4.1 notification style: BigTextStyle. there is a good example of how you can use it. Check it here.
Related
We have code similar to the following in our app
val pendingIntent = PendingIntent.getActivity(ctx, id.toInt(), intent, PendingIntent.FLAG_CANCEL_CURRENT)
val builder = NotificationCompat.Builder(ctx, Channel.TEST_CHANNEL.channelId)
builder.setTicker(tickerText)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setVibrate(vibrate)
.setSmallIcon(icon)
.setAutoCancel(true)
.setLights(-0xff0100, 300, 1000)
.setSound(uri)
.setContentIntent(pendingIntent)
.setStyle(NotificationCompat.BigTextStyle().bigText(contentText))
.addAction(R.drawable.ic_notification, ctx.getString(R.string.notification), piAction)
val notification = builder.build()
val nf = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
nf.notify(NOTIFICATION_TAG, id.toInt(), notification)
}
Starting recently we noticed that notifications on some device running Android 8+ started disappearing briefly after being shown, without user's interaction. Setting auto-cancel to false helps, but the user experience degrades.
The id is a unique item id from the database. This may be important thing to note - technically we can have a notification with such id be shown, removed/canceleld by user, and later some time used again for a similar notification with the same id. Can this be the reason?
We've updated the support libs and tried the following method on builder for luck:
builder.setTicker(tickerText)
...
.setTimeoutAfter(-1)
...
Setting this param to a positive value delayed the notification disappearing by that amount of time (so it did affect). Thus we tried a negative number, the notifications seem to stay there now.
I couldn't find any reasonable documentation explaining this, so this answer is not 100%, but keeping it here for now for others to try and see if it helps them.
Disable your application from auto optimize from battery optimization setting in android OREO. Notification will stay as long as you want
Only thing I found uncertain is NotificationCompat.Builder
Android oreo now uses Notification.Builder instead of NotificationCompat.Builder.
Might be you have to check android version like:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Use Notification.Builder
} else {
// Use NotificationCompat.Builder.
}
I don't think unique id will be an issue for disappearing notification.
Google has created open source sample for this new changes. Please refer to it for more info.
https://github.com/googlesamples/android-NotificationChannels
.setAutoCancel(false)
May be it will work for you.
Tried to show 3 notification in cluster format. As per the doc, I added the setGroupSummary(true) property for the first notification.But in the result i have got only two notification. The notification which is added the GroupSummary property is not visible.
NotificationCompat.Builder firstNotification = createNotification(context,"1.Message","Here you go 1");
firstNotification .setGroupSummary(true);
firstNotification .setGroup("KEY_NOTIFICATION_GROUP");
NotificationCompat.Builder secondNotifi = createNotification(context,"2.Message","Here you go 2");
secondNotifi .setGroup("KEY_NOTIFICATION_GROUP");
NotificationCompat.Builder thirdNotifi= createNotification(context,"3.Message","Here you go 3");
thirdNotifi.setGroup("KEY_NOTIFICATION_GROUP");
Here the notification trigger,
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,firstNotification .build());
notificationManager.notify(1,secondNotifi .build());
notificationManager.notify(2,thirdNotifi.build());
And the result is,
I want to show all three notification in the cluster format without missing.
Any help will be really appreciated.
You should check the following answer :
setgroup() in notification not working
You have to create a separate group notification and set the group summary flag true only for that, and that becomes the parent notification that bundles other notifications with the same group key within itself.
setGroupSummary's purpose is to support API levels below Nougat. On Android 7.0 and higher, it shows a normal group and just uses the on click behavior (setContentIntent) and details like the summary text of the summary notification.
On Android 7.0 and lower, it shows your summary notification as a replacement for all the other notifications the group contains.
Android 7 makes a decision regarding summary notification is shown by itself. So, you want see it unless system decides that it needs to be displayed.
Solution: create a dedicated summary notification.
I an a noob in android, I am trying to show notification of the push notifications I receive. Every time I receive a push notification a new notification is created in the notification bar, even if an exisiting one is present. I want them to be grouped together.
This is what I am currently doing
private void generateNotification(Context context, String ticker, String title, String msg, int icon, Intent intent)
{
int notificationId = 1;
long when = System.currentTimeMillis();
int pendingNotificationsCount = AppName.getPendingNotificationsCount() + 1;
AppName.setPendingNotificationsCount(pendingNotificationsCount);
mNotifyBuilder = new NotificationCompat.Builder(this)
.setWhen(when)
.setContentTitle(title)
.setContentText(msg)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT))
.setNumber(pendingNotificationsCount);
//This prints the count correctly....
Log.d("Snehan", "Message built with Count "+pendingNotificationsCount);
Notification notif = mNotifyBuilder.build();
notif.defaults = Notification.DEFAULT_ALL;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notif);
}
Am I doing something wrong here or missing something??
Seems Android updated the library since I last used it. But the logic is still the same. You need to save whatever the notification id was or at least give it a name you can track and check if it exists. More info can be found in the Android docs. Below is a snippet from what I mean.
To set up a notification so it can be updated, issue it with a notification ID by calling NotificationManager.notify(ID, notification). To update this notification once you've issued it, update or create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the same ID you used previously. If the previous notification is still visible, the system updates it from the contents of the Notification object. If the previous notification has been dismissed, a new notification is created instead.
The docs have everything you need so no need for me to write the code for you :) Hope that helped.
Edit:
Ok, so I recommend you adda a dummy icon just to see what that does. I also recommend instead of chaining all that stuff only chain the text stuff. This way you can debug a bit easier. Try to follow the doc a bit more closely. I don;t really see anything wrong with your code, but obviously something is causing the issue.
Edit 2
So it seems the icon was the problem. I've had this issue before, which is why I mentioned to add that explicitly. Hopefully when someone encounters issues with notifications please make sure you have an icon!!
I'm developing an application for android 2.3.3. I am showing a notification but the text of this notification is too long and its not showing the full text and its being cut so how can I show my full text in my notification?
Here's the code:
String message = "Tonights Taraweeh consists of Alif Lam Mim and the first quarter of Sayaqul. The Surahs covered are Al-Fatiha(The Opening),and the first two-thirds of Al-Baqara(The Cow).";
Notification notification = new Notification();
notification.setLatestEventInfo(context, title, message, contentIntent);
Expanded notifications are only available from Android 4.1, read here
Android 2.3.3 uses the old notifications without expansion. You must user a shorter text, cut your text (and show the full text when user click on it) or adapt the text if you are showing the notification in Android 4.1 or older.
I short you have to set the BigTextStyle
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.new_mail)
.setContentTitle(emailObject.getSenderName())
.setContentText(emailObject.getSubject())
.setLargeIcon(emailObject.getSenderAvatar())
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(__BigTextHere___))
.build();
This question already has answers here:
Android : Notification not working on 2.3.6 (Samsung galaxy y)
(3 answers)
Closed 9 years ago.
My program is calling this code from a service to show notification:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(icon)
.setContentTitle("BU").setOngoing(true).setContentText("BU is the biggest!!!");
mNM.notify(mNotificationId, mBuilder.build());
It is working on Galaxy S-III which has a version 4.1+, but it is not working neither giving an error on android v2.3.6.
I have read the api, but I might miss something. What is the problem and how can I solve it?
Thanks in advance...
Try this code as this is working for me (2.2 and up). I run this method in a seperate service class that I created. Found that my notifications does not work without a background service.
final NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notification = new Notification(R.drawable.notification_popup, message, System.currentTimeMillis());
// used to call up this specific intent when you click on the notification
final PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MyActivity.class), Notification.FLAG_AUTO_CANCEL);
notification.setLatestEventInfo(context, title, message, contentIntent);
notification.defaults = Notification.DEFAULT_ALL;
manager.notify(new Random(100).nextInt(), notification);
Try Notification Compat 2. It has been developed to handle this kind of problem.
From Official documentation Android
This class requires API level 11 or higher.
This document is hidden because your selected API level for the
documentation is 10. You can change the documentation API level with
the selector above the left navigation.
For more information about specifying the API level your app requires,
read Supporting Different Platform Versions.
to confrim this select api level 10 from left side on the page you will get this dialog poped up