如何在后台的android应用程序图标上计算推送通知消息徽章

Posted

技术标签:

【中文标题】如何在后台的android应用程序图标上计算推送通知消息徽章【英文标题】:How to count Push notification message badge on android app icon in background 【发布时间】:2018-02-01 04:53:29 【问题描述】:

我已经在小米设备上运行了我的项目。在 Mi 设备推送通知徽章计数并显示在应用程序图标上,但在另一台设备 sumsung 中,联想不会在应用程序图标上显示推送通知徽章。首先,如何统计后台推送通知消息的数量,以及如何统计这条消息并显示它的应用程序图标。

FirebaseMessaging.java

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService 

    private static final String TAG = FirebaseMessagingService.class.getSimpleName();

    private NotificationUtils notificationUtils;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) 
        super.onMessageReceived(remoteMessage);

        Log.e(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage == null)
            return;

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) 
            Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
            int badge = Integer.parseInt(remoteMessage.getNotification().getBody());
            setBadge(getApplicationContext(), badge);
            handleNotification(remoteMessage.getNotification().getBody());

        

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) 
            Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

            try 
                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                handleDataMessage(json);
             catch (Exception e) 
                Log.e(TAG, "Exception: " + e.getMessage());
            
        

    

    private void handleNotification(String message) 
        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) 
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Constants.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

            // play notification sound
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();
        else
            // If the app is in background, firebase itself handles the notification
        
    

    private void handleDataMessage(JSONObject json) 
        Log.e(TAG, "push json: " + json.toString());

        try 
            JSONObject data = json.getJSONObject("data");

            String title = data.getString("title");
            String message = data.getString("message");
            boolean isBackground = data.getBoolean("is_background");
            // String date = data.getString("date1");
            String imageUrl = data.getString("image");
            String timestamp = data.getString("timestamp");
            JSONObject payload = data.getJSONObject("payload");

            Log.e(TAG, "title: " + title);
            Log.e(TAG, "message: " + message);
            Log.e(TAG, "isBackground: " + isBackground);
            Log.e(TAG, "payload: " + payload.toString());
            Log.e(TAG, "imageUrl: " + imageUrl);
            Log.e(TAG, "timestamp: " + timestamp);


            if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) 
                // app is in foreground, broadcast the push message
                Intent pushNotification = new Intent(Constants.PUSH_NOTIFICATION);
                pushNotification.putExtra("message", message);
                LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

                // play notification sound
                NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
                notificationUtils.playNotificationSound();
             else 
                // app is in background, show the notification in notification tray
                Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
                resultIntent.putExtra("message", message);

                // check for image attachment
                if (TextUtils.isEmpty(imageUrl)) 
                    showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
                 else 
                    // image is present, show notification with image
                    showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl);
                
            
         catch (JSONException e) 
            Log.e(TAG, "Json Exception: " + e.getMessage());
         catch (Exception e) 
            Log.e(TAG, "Exception: " + e.getMessage());
        
    

    /**
     * Showing notification with text only
     */
    private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) 
        notificationUtils = new NotificationUtils(context);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
    

    /**
     * Showing notification with text and image
     */
    private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) 
        notificationUtils = new NotificationUtils(context);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
    

【问题讨论】:

我认为您需要使用自己的逻辑来计算推送通知,例如增加共享偏好中的值等。 如何在 sumsung 设备中发送推送通知消息计数和显示徽章 developer.android.com/preview/features/notification-badges.html 此链接静态显示徽章计数,但我需要在 android 应用上后台计数徽章。 背景计数徽章是什么意思? 【参考方案1】:

您可以使用下面的代码在应用图标上计数

SharedPreferences sharedPreferences = getSharedPreferences(mypreference, MODE_PRIVATE);

    // Read previous value. If not found, use 0 as default value.
    int count = sharedPreferences.getInt("noticount", 0);

    // because you need to show the badge again, it means you need
    // to increment the count
    count = count + 1;

    // Then apply it
    ShortcutBadger.applyCount(getBaseContext(), count);

    // After that, you need to save the value again for another badge count
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("your_key", count);
    editor.commit();
    ShortcutBadger.applyCount(getApplicationContext(), count); 

【讨论】:

以上是关于如何在后台的android应用程序图标上计算推送通知消息徽章的主要内容,如果未能解决你的问题,请参考以下文章

如何在 android 和 ios 收到的推送通知上设置徽章?

在Android O中运行后台运行Intentservice

混合应用推送通知图标未在 Android 8 (Oreo) 上显示

如何在 Android 中禁用多个推送通知图标,我正在使用 Parse.com

如何在 Android + GCM 中获得带有声音 + 自定义应用程序图标的推送通知

屏幕锁定时如何通知推送通知?