long when = Calendar.getInstance().getTimeInMillis();
when += 10000;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
getApplicationContext())
.setWhen(when)
.setContentText(notificationContent)
.setContentTitle(notificationTitle)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(notificationTitle)
.setLargeIcon(largeIcon)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
This runs in the onCreate, however, my notification gets created instantly when the app starts, rather then after 10 seconds.
Whats wrong ?
Even
.setWhen(System.currentTimeMillis()+10000)
does not make it show after 10 seconds. it shows directly.
From the documentation for setWhen(...)...
Add a timestamp pertaining to the notification (usually the time the event occurred). It will be shown in the notification content view by default; use setShowWhen to control this.
The setWhen(...) method doesn't set a time (delay) for when a Notification should be displayed. It's used to show the time that some event has occured.
For example, suppose your app monitors receiving SMS messages - when a message arrives you would create a Notification saying "You have a new SMS message" and you'd use setWhen(...) to show the time that the message was received.
If you want to set a particular delay or fixed time for an event to occur and an associated Notification to be shown, use AlarmManager.
Related
I'm creating a notification with a pendingIntent that opens CallActivity:
NotificationCompat.Builder(context, Constants.CALL_NOTIF_ID)
.setSmallIcon(R.drawable.my_ic)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setSound(null)
.setVibrate(LongArray(0))
.setCategory(NotificationCompat.CATEGORY_CALL)
.setContentIntent(myPendingIntent)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
Im showing this notification while the call is active And clearing it when the call stops with this code:
notificationManager.cancel(Constants.CALL_NOTIF_ID)
Now when I call a device and immediately stop the call, notification appears but it takes 1-2 seconds to disappear. In this short time user can click on notification and can see the CallActivity.
Actually I can make an if check to close activity if there is no call but I don't want to open it in the first place.
So how can I remove the notification immediately without animation (even with reflection)?
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.
a create push notification with next code:
NotificationCompat.Builder b = new NotificationCompat.Builder(context)
.setSmallIcon(smallIcon)
.setTicker(getRewardStr)
.setAutoCancel(false)
.setContentIntent(clickPendingIntent)
.setDeleteIntent(cancelPendingIntent)
.setContentTitle(title)
.setContentText(description)
.addAction(rewardAction)
.setStyle(bigTextStyle)
.setLargeIcon(largeIconBitmap);
in general - everything works ok, i receive all events via broadcast
but I just mention that if notification shows when app is open and than i swipe app from recent, than my notification is canceled but i didn't receive cancelPendingIntent.
is this some kind of default behaviour?
is there any way to catch notification cancel in this situation?
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());
I have a very strange issue, I am working on Push Notification and it was successfully implemented but when i have used BigTextStyle in Notification to show a long message in notification area with setFullScreenIntent() method then the issue coming up the Notification opening the Activity automatically which is set in PendingIntent.
If I don't use setFullScreenIntent() then notification won't opening Activity automatically the user has to tap or click on Notification to open the Activity set in PendingIntent.
So there are two codes
Without setFullScreenIntent() working fine and not opening Activity automatically:
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
With setFullScreenIntent() also working fine but opening Activity automatically:-
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setFullScreenIntent(resultPendingIntent, true) //Whether true or false same result
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
public NotificationCompat.Builder setFullScreenIntent (PendingIntent intent, boolean highPriority)
An intent to launch instead of posting the notification to the status
bar. Only for use with extremely high-priority notifications demanding
the user's immediate attention, such as an incoming phone call or
alarm clock that the user has explicitly set to a particular time. If
this facility is used for something else, please give the user an
option to turn it off and use a normal notification, as this can be
extremely disruptive.
On some platforms, the system UI may choose to display a heads-up
notification, instead of launching this intent, while the user is
using the device.
Parameters
intent: The pending intent to launch.
highPriority: Passing
true will cause this notification to be sent even if other
notifications are suppressed.
Found here. As you can see it immediately launches the intent. I don't really know in what case you wanted to use setFullScreenIntent()?
A notification won't automatically expand when a static notification is displayed on top (could be custom bar with wifi, bluetooth and sound control)
pass setFullScreenIntent and setContentIntent with different pending intents.
Worked for me. click on Notif will work and autolaunch will stop