FIREBASE_MESSAGING:当应用程序处于后台或终止时,onBackgroundMessage 不处理通知

Posted

技术标签:

【中文标题】FIREBASE_MESSAGING:当应用程序处于后台或终止时,onBackgroundMessage 不处理通知【英文标题】:FIREBASE_MESSAGING: onBackgroundMessage not handling notification when app is on Background or Terminated 【发布时间】:2020-09-01 18:33:54 【问题描述】:

我试图让我的 onBackgroundMessage 在应用程序处于后台并且收到通知时执行,但它不执行 myBackgroundMessageHandler。

我完成了可选处理后台消息中文档中所写的所有内容,但它仍然无法像我想要的那样工作, 当我在应用程序处于后台时收到通知时,我得到的是一个没有数据的通知(没有应用程序图标和图像,只有标题和正文)。顺便说一句,当应用程序不在后台时它运行良好

这是我的代码:

androidManifest.xml

<application
        android:name=".Application"

index.js

  message = 
    android: 
        notification:  click_action: 'FLUTTER_NOTIFICATION_CLICK',
    ,
    token: androidNotificationToken,
    data: 
      activityFeedItemId:activityFeedItemId,
      userReceivingNotificationId: userId,
      userActivatingNotificationPhotoUrl: activityFeedItem.userProfileImg,
      notificationType: activityFeedItem.type,
      body : body
    
  ;

buildgradle

dependencies 
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-analytics:17.2.2'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.firebase:firebase-messaging:20.1.7'


apply plugin: 'com.google.gms.google-services'

MainActivity.java

package com.yimerah.ijn_amen;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity 
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) 
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    

Application.java

package com.yimerah.ijn_amen;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;

public class Application extends FlutterApplication implements PluginRegistrantCallback 
    @Override
    public void onCreate() 
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    

    @Override
    public void registerWith(PluginRegistry registry) 
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    

OnBackgroundMessage

  onBackgroundMessage: myBackgroundMessageHandler
   static myBackgroundMessageHandler
    final FirebaseUser firebaseUser = await auth.currentUser();

    print("on message:$message\n");
    final String userReceivingId =
        message['data']['userReceivingNotificationId'];
    final String body = message['data']['body'];
    final String notificationType = message['data']['notificationType'];
    final String notificationId = message['data']['activityFeedItemId'];
    if (userReceivingId == firebaseUser.uid) 
      int id = Uuid().parse(notificationId).reduce((a, b) => a + b);
        final String notificationMedia =
            message['data']['userActivatingNotificationPhotoUrl'];
        await showNotificationMediaStyle("", body, notificationMedia, id: id);


      print("Notification shown!");
    
    print("Notification not shown!");
  

提前感谢您的帮助,

【问题讨论】:

【参考方案1】:

构造数据通知时,notification 有效负载必须为空。

如果notification 负载不为空,则负载将作为通知而不是数据消息发送。

【讨论】:

谢谢!它正在工作。我还必须从 backgroundMessageHandler 中删除 if 语句(userReceivingId == firebaseUser.uid),因为当应用程序处于 onBackground 时,它无法获取 firebaseUser.uid,因此处理后台消息数据.. @ray-li 你能分享一下样本payload吗【参考方案2】:

这是应用程序在后台时的预期行为,通知直接进入系统托盘,而不会调用onMessageReceived

更多信息:https://firebase.google.com/docs/cloud-messaging/android/receive

【讨论】:

所以我必须实现 onMessageReceived ?但如何?文档似乎不适应颤动...... 即使该方法被调用 onBackgroundMessage 或 onMessageReceived 该文档说如果应用程序在后台将不会调用该方法。 然后呢?这是否意味着无法在后台处理通知? 是的,通知直接进入系统托盘。

以上是关于FIREBASE_MESSAGING:当应用程序处于后台或终止时,onBackgroundMessage 不处理通知的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 推送通知应用背景:firebase_messaging

firebase_messaging onResume 和 onLaunch 不起作用

firebase_messaging 如何清除通知?

firebase_messaging :没有通知通过

使用颤振 firebase_messaging 插件发送通知声音

无法使用 firebase_messaging 在 iOS 上的 Flutter 应用中订阅主题