FCM android客户端随机停止接收数据消息

Posted

技术标签:

【中文标题】FCM android客户端随机停止接收数据消息【英文标题】:FCM android client randomly stops receiving data messages 【发布时间】:2021-01-21 10:10:39 【问题描述】:

我已经使用 android 客户端在我的服务器上实现了 FCM,并且它运行了一段时间。突然之间,客户端一次停止接收通知 5-10 分钟,之后它会立即收到所有待处理的通知。日志显示服务器正在正确发送消息。 当消息队列卡住时,甚至来自 Firebase 控制台的测试消息都无法通过。

我在官方文档中发现的唯一相关内容是关于the lifetime of a message:

如果设备未连接到 FCM,则会存储消息,直到建立连接(再次遵守折叠密钥规则)。建立连接后,FCM 会将所有待处理消息传递给设备。

但是我的网络运行良好,而且这个问题也出现在其他网络和客户端上。当我全新安装该应用程序时,我的服务器正在生成并成功接收新令牌。只是服务器到客户端的消息没有通过。

如何判断连接是否建立并修复?或者还有什么原因造成的?

这是我的客户端代码

AndroidManifest.xml

        <application
        android:name=".App"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning">
        <service
            android:name=".service.NotificationService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

NotificationService.java

public class NotificationService extends FirebaseMessagingService 

    private static final String TAG = "NotificationService";
    private static String token;

    public NotificationService() 
        token = PreferenceManager
                .getDefaultSharedPreferences(App.getContext())
                .getString("firebase-token", null);
    

    @Override
    public void onNewToken(@NonNull String s) 
        super.onNewToken(s);
        FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() 
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) 
                        if (!task.isSuccessful()) 
                            Log.w(TAG, "getInstanceId failed", task.getException());
                            return;
                        
                        token = task.getResult().getToken();
                        Log.d(TAG, "onComplete: token = " + token);
                        PreferenceManager
                                .getDefaultSharedPreferences(App.getContext())
                                .edit()
                                .putString("firebase-token", token)
                                .apply();
                    
                );
    

    @Override
    public void onMessageReceived(final RemoteMessage remoteMessage) 
        super.onMessageReceived(remoteMessage);
        Log.d(TAG, "Received message of size " + remoteMessage.getData().size());
        if (remoteMessage.getData().size() > 0) 
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            Handler h = new Handler(Looper.getMainLooper());
            h.post(new Runnable() 
                public void run() 
                    int left = -1, right = -1;
                    if (remoteMessage.getData().get("left") != null) 
                        left = Integer.parseInt(Objects.requireNonNull(remoteMessage.getData()
                                .get("left")));
                    
                    if (remoteMessage.getData().get("right") != null) 
                        right = Integer.parseInt(Objects.requireNonNull(remoteMessage.getData()
                                .get("right")));
                    


                    if (remoteMessage.getData().get("REFRESH") != null) 
                        Util.sendTitleBroadcast(getApplicationContext(),
                                left, right,
                                Util.REFRESH_TITLE);
                     else 
                        Util.sendTitleBroadcast(getApplicationContext(),
                                left, right,
                                Util.TITLE_DATA_RECEIVED);
                    
                
            );
        
    

    public static String getToken() 
        if (token == null) 
            token = PreferenceManager
                    .getDefaultSharedPreferences(App.getContext())
                    .getString("firebase-token", null);
        
        return token;
    

服务器端代码 FirebaseMessagingService.java

public class FirebaseMessagingService 

    private static final Logger logger = Logger
            .getLogger(FirebaseMessagingService.class);
    private static FirebaseMessagingService instance;

    /**
     * Path to resource file that contains the credentials for Admin SDK.
     */
    private final String keyPath = "firebase-adminsdk.json";

    /**
     * Maps a token list to each topic Object.
     */
    private static Map<Object, List<String>> topicTokenMap;

    public synchronized static FirebaseMessagingService getInstance() 
        if (instance == null) 
            instance = new FirebaseMessagingService();
        
        return instance;
    

    private FirebaseMessagingService() 
        init();
    

    /**
     * Initializes the Firebase service account using the credentials from the
     * json file found at keyPath.
     */
    private void init() 
        try 
            InputStream serviceAccount
                    = getClass().getClassLoader().getResourceAsStream(keyPath);
            if (serviceAccount != null) 
                FirebaseOptions options = new FirebaseOptions.Builder()
                        .setCredentials(GoogleCredentials
                                .fromStream(serviceAccount))
                        .setDatabaseUrl(databaseUrl)
                        .build();

                FirebaseApp.initializeApp(options);
                logger.debug("FirebaseMessagingService: init successful");
             else 
                logger.debug("FirebaseMessagingService: Input stream null from"
                        + " path: " + keyPath);
            
         catch (IOException ex) 
            logger.debug("FirebaseMessagingService: Failed to get credentials "
                    + "from inputStream." + ex.getMessage());
        
    

    /**
     * Sends the messages given as parameters to the list of tokens mapped to
     * the given topic Object.
     *
     * @param topic
     * @param messages
     */
    public void sendMessage(Object topic,
            Map<String, String> messages) 
        logger.debug("FirebaseMessagingService: sending Message "
                + messages.toString());
        try 
            if (topicTokenMap != null) 
                List<String> tokenList = topicTokenMap.get(topic);
                if (tokenList != null) 
                    tokenList.removeAll(Collections.singleton(null));
                    MulticastMessage message = MulticastMessage.builder()
                            .putAllData(messages)
                            .addAllTokens(tokenList)
                            .build();
                    FirebaseMessaging.getInstance().sendMulticast(message);
                    logger.debug("FirebaseMessagingService: message sent successfully");
                
            
         catch (FirebaseMessagingException ex) 
            logger.debug(ex.getMessage());
        
    

    /**
     * Registers the token given as a parameter to the topic Object.
     *
     * @param topic
     * @param token
     */
    public void registerTokenToTopic(Object topic, String token) 
        if (topicTokenMap == null) 
            topicTokenMap = new HashMap<Object, List<String>>();
        
        List<String> tokens = topicTokenMap.get(topic);
        if (tokens == null) 
            tokens = new ArrayList<String>();
        
        if (!tokens.contains(token)) 
            tokens.add(token);
        
        topicTokenMap.put(topic, tokens);
    

    /**
     * Unregisters all instances of the token given as a parameter from the topic
     * given.
     *
     * @param topic
     * @param token
     */
    public void unregisterTokenFromTopic(Object topic, String token) 
        if (topicTokenMap != null) 
            List<String> tokens = topicTokenMap.get(topic);
            if (tokens != null) 
                tokens.removeAll(Collections.singleton(token));
            
        
    

    /**
     * Looks for a topic that has a field with the name equal to topicFieldName
     * and the value equal to topicFieldValue and sends a notification to the
     * tokens subscribed to that topic, telling them to ask for an update.
     *
     * @param topicFieldName
     * @param topicFieldValue
     */
    public void notifyChangeForTopicField(String topicFieldName,
            Object topicFieldValue) 
        logger.debug("FirebaseMessagingService: notifyChangeForTopicField: "
                + "topicFieldValue = " + topicFieldValue.toString());

        Map<String, String> messageMap = new HashMap<String, String>();
        messageMap.put("REFRESH", "true");

        if (topicTokenMap != null
                && topicTokenMap.entrySet() != null
                && topicTokenMap.entrySet().size() > 0) 
            Iterator it = topicTokenMap.entrySet().iterator();
            while (it.hasNext()) 
                try 
                    Map.Entry pair = (Map.Entry) it.next();
                    Object topic = pair.getKey();
                    logger.debug("FirebaseMessagingService: "
                            + "notifyChangeForTopicField topic = " + topic.toString());
                    Field field = topic.getClass().getDeclaredField(topicFieldName);
                    logger.debug("FirebaseMessagingService: "
                            + "notifyChangeForTopicField field = " + field.toString());
                    field.setAccessible(true);
                    logger.debug("FirebaseMessagingService: "
                            + "notifyChangeForTopicField field contains topic: "
                            + (field.get(topic) != null ? "true" : "false"));
                    if (field.get(topic) != null
                            && field.get(topic).equals(topicFieldValue)) 
                        sendMessage(topic, messageMap);
                        break;
                    
                    it.remove();
                 catch (NoSuchFieldException ex) 
                    java.util.logging.Logger.getLogger(FirebaseMessagingService.class.getName()).log(Level.SEVERE, null, ex);
                 catch (SecurityException ex) 
                    java.util.logging.Logger.getLogger(FirebaseMessagingService.class.getName()).log(Level.SEVERE, null, ex);
                 catch (IllegalArgumentException ex) 
                    java.util.logging.Logger.getLogger(FirebaseMessagingService.class.getName()).log(Level.SEVERE, null, ex);
                 catch (IllegalAccessException ex) 
                    java.util.logging.Logger.getLogger(FirebaseMessagingService.class.getName()).log(Level.SEVERE, null, ex);
                
            

        
    

编辑:经过几次测试后,我发现当一个客户端停止工作时,其他客户端可以正常接收消息

【问题讨论】:

【参考方案1】:

你的onMessageReceived() 在我看来有点矫枉过正。我认为您不必创建自己的处理程序来处理数据。只需坚持 Firebase 示例:FirebaseExample

override fun onMessageReceived(remoteMessage: RemoteMessage) 
    if (remoteMessage.data.isNotEmpty()) 
        Log.d(TAG, "Message data payload: $remoteMessage.data")
        if (/* Check if data needs to be processed by long running job */ true) 
          // For long-running tasks (10 seconds or more) use WorkManager.
          scheduleJob()
         else 
            // Handle message within 10 seconds
            handleNow()
        
    

【讨论】:

嘿,谢谢。 Tbh 我不记得当时制作处理程序背后的逻辑是什么,但是,我的问题是消息永远不会到达客户端,并且永远不会调用 onMessageReceived。 好吧,也许你可以提高优先级:link 我已经读过这方面的信息,但我使用 FCM 作为计数器。用户扫描项目,服务器处理项目,并将更新后的计数器发送回已处理的项目数量。如果应用程序在后台,用户不需要收到更改通知,并且用户在使用应用程序时不会在查看通知之外与通知进行交互。”高优先级消息通常应该导致用户与“我认为这些消息会很快被取消优先级 另外,“默认情况下,通知消息以高优先级发送,数据消息以正常优先级发送。”当我从 Firebase 控制台发送测试消息时,我正在发送通知,因此具有隐含的高优先级,并且它们无法通过。我在我的初始 q 中添加了一个编辑,我用 3 台设备对其进行了测试,其中一台卡住了 10 分钟,而另外两台运行良好。 据我了解您的意图,我认为您滥用了 FCM。我会使用 Firestore(FS) 和 CloudFunctions(CF)。您可以trigger 来自您的客户的 CF。 CF(在服务器端)可以处理数据并设置存储在 FS 中的计数器。您的客户正在收听 FS,可以在 realtime 中更新。【参考方案2】:

我在这里找到了答案:https://***.com/a/23902751/10702209 发生的事情是我在 5 分钟后随机出现路由器超时。我已通过在 4 分钟后手动向 FCM 发送心跳来解决此问题,而不是默认的 15 分钟。

context.sendBroadcast(new Intent("com.google.android.intent.action.GTALK_HEARTBEAT"));
context.sendBroadcast(new Intent("com.google.android.intent.action.MCS_HEARTBEAT"));

几个月前我已经实施了这个修复程序,从那以后再也没有出现过这个问题。

【讨论】:

以上是关于FCM android客户端随机停止接收数据消息的主要内容,如果未能解决你的问题,请参考以下文章

添加 FCM 支持后,本地通知在 iOS 上停止工作

消息接收的Android后台服务

接收新的FCM消息(前台和后台)时Android应用程序崩溃

是否需要在FCM中修剪设备?

在 Android 上的代理后面接收 FCM (GCM)

Firebase 云消息传递如何在 Android 上运行