gpt4 book ai didi

flutter - FIREBASE_MESSAGING : onBackgroundMessage not handling notification when app is on Background or Terminated

转载 作者:行者123 更新时间:2023-12-04 12:19:33 34 4
gpt4 key购买 nike

当应用程序处于后台并且收到通知时,我试图让我的 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);
}
}

应用程序.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!");
}

预先感谢您的帮助,

最佳答案

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

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

关于flutter - FIREBASE_MESSAGING : onBackgroundMessage not handling notification when app is on Background or Terminated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61827613/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com