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



我正在尝试让我的onBackgroundMessage在应用程序处于后台状态并且收到通知时执行,但它没有执行myBackgroundMessageHandler。

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

这是我的代码:

AndroidManifest.xml
<application
android:name=".Application"

索引.js

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

构建格勒

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'

主活动.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);
}
}

应用.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: myBackgroundMessageHandler
static myBackgroundMessageHandler{
final FirebaseUser firebaseUser = await auth.currentUser();
print("on message:$messagen");
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!");
}

提前感谢您的帮助,

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

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

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

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

使用 Firebase/Flutter 的 onBackgroundMessage 功能,您只能打印消息或使用 Flutter Local Notifications 来显示消息。

如果要执行任何其他操作(例如更新应用状态或发出 API 请求(,则不会立即使用。

我不会给出完整的编码答案,但解决方法是使用 Dart 隔离和隔离通信。请在这里进行自己的研究,但很容易实现。

这是因为Firebase 后台处理程序在无法访问 Flutter 应用隔离和内存的单独隔离上运行。

最新更新