2017-06-19 18 views
0

我发现这个库(https://github.com/leolin310148/ShortcutBadger)在android中实现应用图标计数器徽章。 徽章在我活动时执行。但我希望在收到推送通知时执行此操作。我目前在Firebase消息服务中的onMesseageReceived()方法中初始化代码,但它不起作用。FCM onReceived方法实现快捷方式Badger库

看到下面的代码: 感谢您的帮助

@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) { 

} 

}

回答

0

经过大量的研究,我理解执行这一快捷徽章是不在FCM服务中工作。我可以通过每次收到推送通知时自动从FCM服务调用一个自定义服务来解决此问题,并且自定义服务处理徽章代码,并且此类问题会自行破坏。

这种方式是完美的。