FCM onReceived方法实现Shortcut Badger库

时间:2017-06-19 17:46:36

标签: android notifications push counter

我发现这个库(https://github.com/leolin310148/ShortcutBadger)在android中实现app图标计数器徽章。 我在活动中实施徽章。但是我希望在收到推送通知时实现这一点。我目前在Firebase消息服务中的onMesseageReceived()方法中有初始化代码,但它无法正常工作。

见下面的代码: 谢谢你的帮助

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Logger.i("Received message");




    //Shourtcut Badger
    int badgeCount = 1;
    ShortcutBadger.applyCount(context, badgeCount);



    //parseNotification(bundle);
    parseNotification(remoteMessage.getData());
    if (remoteMessage.getData() != null) {


        String str = remoteMessage.getData().get("notification");
        String from_user = "";
        String message = "";
        String name = "";
        int type = 0;
        if (str != null && str.length() > 0) {


            try {
                JSONObject jobj = new JSONObject(str);

                from_user = jobj.optString("from");
                message = jobj.optString("message");
                name = jobj.optString("name");

                type = Integer.parseInt(jobj.optString("type"));



                if (type == LIKE_STATUS) {
                    handleStatusLikeByFriendPush(jobj, "" + type);
                } else if (type == PHOTO_UPDATE) {
                    handlePhotoUpdateByFriendPush(jobj, "" + type);


            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            message = "";//bundle.getString("message");
            from_user = "";//bundle.getString("source");

            if (from_user != null && from_user.contains(IMHandler.AT + IMHandler.DOMAIN)) {
                from_user = from_user.substring(0, from_user.indexOf(IMHandler.AT));
            }

            showNotification(from_user, message, type, name);

        }



    }



}










public void handleStatusLikeByFriendPush(JSONObject jobj, String pushType) {
 // method 

}

public void handlePhotoLikeByFriendPush(JSONObject jobj, String pushType) {

//方法工具     }

}
private void showNotification(String from, String body, int type, String name) {

}

}

1 个答案:

答案 0 :(得分:0)

经过大量研究后,我了解此快捷方式徽章的实施在FCM服务中无效。我可以通过在每次收到推送通知时从FCM服务调用自定义服务来解决此问题,并且自定义服务处理徽章代码并且此处会破坏自身。

这种方式非常有效。