当应用程序在后台运行时,不会触发onMessageReceived方法 [英] When an app is in background, onMessageReceived method is not firing

查看:404
本文介绍了当应用程序在后台运行时,不会触发onMessageReceived方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我需要在应用程序图标中显示徽章,但是我不知道如何获取徽章,以及为什么当应用程序在后台或应用程序未运行时onMessageReceived无法调用.

Actually i need to show badge in app icon but i don't know how to get badge and why onMessageReceived not call when app is in background or app is not running.

public class Custom_FirebaseMessagingService extends FirebaseMessagingService          {
            private static final String TAG = "FirebaseMsgService";
            String activityName;

            @Override
            public void onMessageReceived(RemoteMessage remoteMessage) {

                if (remoteMessage == null)
                    return;
                if (remoteMessage.getNotification() != null) {
                    Log.i(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
                }
                // Check if message contains a data payload.
                if (remoteMessage.getData().size() > 0) {
                    Log.i(TAG, "Data Payload: " + remoteMessage.getData().toString());
                    try{
                        //boolean from_bg = Boolean.parseBoolean(remoteMessage.getData().get("from_bg").toString());
                        Map<String, String> data = remoteMessage.getData();
                        boolean show_fg =  Boolean.parseBoolean(data.get("show_fg"));
                        ..... 

推荐答案

FCM有两种类型

notification消息:仅当您的应用程序处于前台状态时,发送具有此消息类型的有效负载才会触发onMessageReceived().

notification Messages: Sending a payload with this message type triggers onMessageReceived() only when your app is in foreground.

data消息:仅使用 这种特定消息类型发送有效负载会触发onMessageReceived(),无论您的应用程序是在前台/后台.

data Messages: Sending a payload with only this specific message type triggers onMessageReceived() regardless if your app is in foreground/background.

参考: https://firebase.google.com/docs /cloud-messaging/android/receive#handling_messages

这篇关于当应用程序在后台运行时,不会触发onMessageReceived方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆