我需要在通知面板中显示通知以及应用程序图标中的徽章计数

时间:2017-03-01 13:51:58

标签: android

我需要在通知面板中显示通知以及应用图标中的徽章计数。谢谢。

使用以下库

me.leolin:ShortcutBadger:1.1.13@aar

但仅在应用处于有效状态时显示徽章计数

我的代码:

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        ShortcutBadger.applyCount(getApplication(), badgeCount); //for 1.1.4+
        badgeCount++;
        startService(new Intent(this, BadgeService.class));
        stopService(new Intent(getApplication(), BadgeService.class));

        Intent intent=new Intent();
        intent.setAction("SOME_ACTION");
        intent.putExtra("message", remoteMessage.getNotification().getBody());
        sendBroadcast(intent);



        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
            try {
                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                sendPushNotification(json);
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    }

    private void sendPushNotification(JSONObject json) {

        try {
            //getting the json data
            JSONObject data = json.getJSONObject("data");

            //parsing json data
            String title = data.getString("title");
            String message = data.getString("message");
            String imageUrl = data.getString("image");
            MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());


            Intent intent = new Intent(getApplicationContext(), HomeActivity.class);


            if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.LOLLIPOP){

                mNotificationManager.showNotification(getApplicationContext(),title, message, intent);
            }

            if(imageUrl.equals("null")){

                mNotificationManager.showSmallNotification(title, message, intent);
            }else{

                mNotificationManager.showBigNotification(title, message, imageUrl, intent);
            }

0 个答案:

没有答案