Notification Count Badge in Android App Icon

2019-04-09 18:58发布

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?

3条回答
我命由我不由天
2楼-- · 2019-04-09 19:16

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-09 19:36

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.

查看更多
叛逆
4楼-- · 2019-04-09 19:38

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.

查看更多
登录 后发表回答