Notification Count Badge in Android App Icon

2019-04-09 19:28发布

问题:

I am developing and android application, where I am sending notifications to the user using Firebase Cloud Messaging Service. In order to make user more aware of notifications, I want to show the count of the notifications on the launcher icon of the app. Is there any way how can I do it? I don't want to create a widget. I have tried the library ShortcutBadger Link for the Library

But the issue with this library is that it is not working when the app is in background or killed?

I have used the count update method in the BroadcastReceiver of my app

Can someone suggest how to use it or a better way of doing it?

回答1:

Android O introduces notification badges:

https://developer.android.com/preview/features/notification-badges.html

They show the number of notifications with a long-press on the icon. It will be awhile before O has widespread adoption, though.



回答2:

If you have your own FirebaseMessagingService, you could set your badge count in onCreate.

public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onCreate() {
    super.onCreate();
    ShortcutBadger.applyCount(MyFirebaseMessagingService.this, 1);
 }
}

onCreate will be fired when the notification arrives. I hope it helps you.



回答3:

So, In order to take some action in app when a notification is received irrespective of the state of the app, then we need to send data type of notifications. onMessageReceived() method of FireabaseMessagingService is triggered always irrespective of the app state is the user is sent data type of notifications, else if the notification object also contains notification field then the system tray itself handles the population of notification in the device and app has no track of it.