Problem
When using the android emulator coming with Android studio 2.2.3, a notification is displayed in the status bar just for a barely noticeable moment, then it disappears. When using a real device, the code works as expected - the icon remains visible in the status bar.
The code
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification n = new Notification.Builder(this)
.setContentIntent(null)
.setContentTitle("Title")
.setContentText("Text")
.setSmallIcon(R.drawable.ic_alert)
.setAutoCancel(false)
.build();
nm.notify(12345, n);
Any ideas?
It is because setPriority() by default is PRIORITY_DEFAULT = 0.
If you setPriority(Notification.PRIORITY_MAX). Then the notification will be visible for couple of seconds before hiding.
As the priority increases significance of attention to notification increases.
Just wondering how is your device behaving otherwise.
Related
I am using the following code to show my notification:
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context,
context.getString(R.string.notification_general_id))
.setSmallIcon(R.drawable.iit)
.setContentTitle("something")
.setContentText(notification)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
// https://stackoverflow.com/a/28251192/2287994
long time = new Date().getTime();
String tmpStr = String.valueOf(time);
String last4Str = tmpStr.substring(tmpStr.length() - 5);
int notificationId = Integer.parseInt(last4Str);
Log.d(TAG, "notificationId " + notificationId);
notificationManager.notify(notificationId, builder.build());
I create notification channel using the following code:
// for showing general notifications
NotificationChannel generalNotificationsChannel = new NotificationChannel(
getString(R.string.notification_general_id),
getString(R.string.notification_general_name),
NotificationManager.IMPORTANCE_HIGH
);
nearbyAnchorsChannel.setDescription(getString(R.string.notification_general_desc));
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
On Google Pixel 3a, the notification only vibrates and shows up as a blip on the top left. However, on the OnePlus 6, it shows up as a proper heads-up notification that we can immediately swipe or tap on. I tried looking through the settings of my Pixel 3a (it is Android 12) but I cannot find any option that I can change to enable a heads-up display of notifications. Tbh, I am not even sure if there is something wrong with my code or the phone I am testing it on. Is it because of my OnePlus 6's Android version (it is Android 11)? Or is it due to the code that I have written? If it is due to the former then can someone please explain to me how I can change settings on my Pixel 3a to show a proper swipeable heads-up notification?
My bad, when you change channel importance, you need to uninstall the app and install it again. That's why it worked on the OnePlus 6 (because I installed it after changing the notification importance) and not on the Google Pixel 3a (because I was still working with the same install)
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.
my code is below:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("nothing")
.setContentText("nothing").setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("help help help:)")
.setContentInfo("nothing")
.build();
manager.notify(1, notification);
When I run this, "help help help:)" didn't appear.
TickerText will only appear on phone before Android 5.0 (L)
Extracted from Documentation :
Text that summarizes this notification for accessibility services. As
of the L release, this text is no longer shown on screen, but it is
still useful to accessibility services (where it serves as an audible
announcement of the notification's appearance).
https://developer.android.com/reference/android/app/Notification.html#tickerText
So I have set up a 2-page Android wear notification (dispatched from the device) as such:
final NotificationCompat.Builder mainNotificationBuilder = new NotificationCompat.Builder(this)
.setDefaults(android.app.Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.icon)
.setStyle(bigStyle)
.setContentIntent(contentIntent);
android.app.Notification secondNotification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(balanceBackgroundImage)
.setStyle(bigStyle2)
.build();
android.app.Notification twoPageNotif = mainNotificationBuilder
.extend(new NotificationCompat.WearableExtender()
.setContentIcon(R.drawable.icon)
.setBackground(backgroundImage)
.addPage(secondNotification)
)
.setSmallIcon(R.drawable.icon)
.build();
However, the small icon only appears on the first page, but I want it to appear on both!
Is this even possible? I have a feeling that its not, and that the icon can only appear on the first page of a multi-page watch notification, but I wanted to get confirmation from you all.
Thanks in advance :\
Your thinking is correct: the small icon only appears on the first page of a notification.
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();