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

问题描述 投票:0回答:2

我正在尝试让我的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!");
  }

感谢您的帮助,

我正在尝试让我的onBackgroundMessage在应用程序处于后台且已收到通知的情况下执行,但它不执行myBackgroundMessageHandler。我所做的一切都是...

flutter firebase-cloud-messaging android-notifications
2个回答
0
投票

这是应用程序在后台运行时的预期行为,通知直接发送到系统托盘,而无需调用onMessageReceived


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.