简体   繁体   English

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

[英]FIREBASE_MESSAGING: onBackgroundMessage not handling notification when app is on Background or Terminated

I'm trying to get my onBackgroundMessage to execute when the app is on Background and a notification has been receive, but it doesn't execute myBackgroundMessageHandler.我试图让我的 onBackgroundMessage 在应用程序处于后台并且已收到通知时执行,但它不执行 myBackgroundMessageHandler。

I did everything that was written in the documentation at Optionally handle background messages, but it still doesn't work like i want, When i receive a notification when the app is on Background, What i get is a notification without data (no app icon and no images, only title and body text).Btw it's working well when the app is not onBackground我做了文档中写的所有内容,可选地处理后台消息,但它仍然不能像我想要的那样工作,当我在应用程序处于后台时收到通知时,我得到的是没有数据的通知(没有应用程序图标没有图像,只有标题和正文)。顺便说一句,当应用程序不在后台时它运行良好

Here my code:这是我的代码:

AndroidManifest.xml

<application
        android:name=".Application"

index.js索引.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 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应用.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!");
  }

Thank you in advance for your help,预先感谢您的帮助,

When constructing a data notification, the notification payload MUST be blank.构造数据通知时, notification负载必须为空。

If the notification payload is not empty, then the payload will be sent as a notification instead of a data message.如果notification负载不为空,则负载将作为通知而不是数据消息发送。

That is the expected behavior when the app is in the background, the notification goes directly to the Sytem Tray without onMessageReceived being called这是当应用程序在后台时的预期行为,通知直接进入系统托盘而不调用onMessageReceived

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 应用程序终止时未调用消息后台处理程序(颤振) - Firebase messaging background handler not called when app is terminated (Flutter) firebase 消息处理与本地通知 - firebase messaging handling with local notification Firebase app在后台时如何处理通知 - How to handle notification when app in background in Firebase 如何使用 react 在 firebase-messaging-sw.js 中的 onBackgroundMessage 触发 App.tsx 的事件 - How to trigger an event at App.tsx from onBackgroundMessage in firebase-messaging-sw.js with react Flutter Firebase 消息 - 应用打开时不显示推送通知 - Flutter Firebase messaging - push notification is not showing when app is open Flutter 错误:错误:无法解析 'package:firebase_messaging/firebase_messaging.dart' 中的 package 'firebase_messaging' - Flutter Error: Error: Could not resolve the package 'firebase_messaging' in 'package:firebase_messaging/firebase_messaging.dart' messages.onBackgroundMessage 不是 function - messaging.onBackgroundMessage is not a function Flutter with firebase_messaging - 如何在权限对话框中自定义消息? - Flutter with firebase_messaging - how to custom message on permission dialog? firebase_messaging/未知:Flutter IOS 发生未知错误 - firebase_messaging/unknown: An unknown error has occured on Flutter IOS 通过 api 显示应用程序在后台时收到的 Firebase 消息通知 - Show Notification of Firebase Message onReceived when App is in Background Through api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM