I want to update app icon badge count without push notifications(eg. Silent push). I simply want to update app badge count after reading any notifications inside the app similar to linkedIn, where its updating the count on app badge when am reading any notification. Currently I've implemented the solution where I am having payload from FCM with badge count and updating it with android notification builder with silent push notification channel configured.
var notificationBuilder = NotificationCompat.Builder(this#MainActivity, CHANNEL_ID)
.setContentTitle("New Messages")
.setContentText("You've received 3 new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setNumber(messageCount)
.build()
but that is not gonna work for me as I also need to update the count once I've read any notification inside the app. If I clear the notification from notification tray then count is also disappearing from app badge, is there any way that I can set that count without push notification within the app? Thanks in advance.
So after working around badge count update feature so long, I've come to conclusion that Android do not provide any api to update badge count on app icon without notification. In android we can internally send notification and with the help of this we can update badge count.
So we need to notify the device for updating badge count, without notifying we can't update it. If we are sending count to Notification Builder then we just need to take care to update badge count to keep the notification id same every time if we want to update the badge count otherwise it will add count to its previous notification. Just keep notification id same and create a separate channel with silent push to update badge count.
public static int NOTIFICATION_ID = 1001;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
that solution will override previous count.
Related
how can we show a badge count on app icon when the app is closed / at background? As far as I know, I can use the third party lib (like ShortcutBadger)to set the count on the app icon but the problem is when android app is at background, notification is handled at the system tray, and my code will not be processed (please notice me if I am wrong).
how can I set the count number on app icon??
you can use something like this
Notification notification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setContentTitle("New Messages")
.setContentText("You've received 3 new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setNumber(messageCount)
.build();
.setNumber(messageCount) will be added as luncher icon badge in api level 26
How can I remove all notifications in the notification tray that were sent by my server when the user clicks on one notifiaction?
I have a chatting-app and the user gets a notification for each message (if the app is not in foreground). When the user clicks on one of those notifications the app will be brought to foreground / will be started. After that has happened, I want all other notifications in the notification bar to disappear as well.
No, you dont have to save the ids of all notifications, you can simply call:
NotificationManager nManager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
nManager.cancelAll();
Use the following code to cancel a Notification:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
If you have different notifications that need to be canceled you have to save the ids that you used to create the Notification.
I am building a messaging application that notifies users when a new message comes in.
Because this could happen several times a day (or several times an hour), I don't want to continually throw new notifications. Instead, if the user has not dismissed a notification, I would like to update it with the number of new messages pending (following the "Stacking" design guideline).
In the Android documentation, there is an example of updating a notification with a number:
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
...
HOWEVER, this seems to assume that you are maintaining this number within your application and outside of the notification manager / builder. For a host of reasons, this is very inconvenient (and brittle) in the context of my application.
I would like to know - is there any way to read the current number assigned to a message (the equivalent of mNotifyBuilder.getNumber()) ?
FOLLOW-ON QUESTION: If reading the current number is not possible, is there a way to know from a running service if a notification has been cancelled or manually dismissed by the user ?
You notification is assigned with an ID. You can use this ID to cancel or update the same notification if it hasn't been cancelled by the user.
Straight from the documentation: "If a notification with the same id
has already been posted by your application and has not yet been
canceled, it will be replaced by the updated information."
See this post and that one.
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
I'm looking at this code in the Android documentation for stacking notifications:
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
...
but I don't understand how you can keep track of the numMessages parameter across multiple receipts from GCM or how you can start a "loop that processes data" as they say in comments. I would think you would need to retrieve any current notifications and then append the new data to them. Any help appreciated.
edit: I also don't understand what the point of iterating over the loop would be if each notification overwrites the last one, why not just send a notification for the last iteration of this loop?
Try it:
new Notification.Builder().setAutoCancel(false).setOngoing(false);
Good luke!
mBuilder.setAutoCancel(true);//automatically canceled when the user clicks it
mBuilder.setOngoing(true);/*if true:
Ongoing notifications differ from regular notifications in the following ways:
Ongoing notifications are sorted above the regular notifications in the notification panel.
Ongoing notifications do not have an 'X' close button, and are not affected by the "Clear all" button. */