Notification Count Badge in Android App Icon

做~自己de王妃 提交于 2019-12-06 05:39:40

问题


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.



来源:https://stackoverflow.com/questions/45064184/notification-count-badge-in-android-app-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!