【问题标题】:Flutter 2.0 with Firebase Cloud Messaging: onMessage not called on Android带有 Firebase 云消息传递的 Flutter 2.0:onMessage 未在 Android 上调用
【发布时间】:2021-06-16 10:53:39
【问题描述】:

我在 Flutter 2.0 中遇到了 Firebase 云消息传递 onMessage 的问题。

功能

FirebaseMessaging.onMessage.listen((RemoteMessage message) { ... }

在前台接收消息时不调用。但是,日志说

收到消息的广播

在收到第一条消息时,我会收到其他警告,例如:

访问隐藏方法 Landroid/os/WorkSource

但警告会在后续消息中消失。

有趣的事情是,

FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

有效。

如果我将应用程序发送到后台,我会收到通知并调用定义的方法。

代码

@override
void initState() {
    initializeFlutterFire()
        .then((value) => subscribeToMessages);

    super.initState();
}

Future<void> initializeFlutterFire() async {
    try {
        Firebase.initializeApp();
        setState(() {
            _initialized = true;
            print("Firebase has been initialized");
        });
    } catch (e) {
       setState(() {
           _error = true;
       });
    }
}

void subscribeToMessages() {
    // The following handler is called, when App is in the background.
    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

    // The following function is not called, when a message was received by the device.
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        print('Got a message whilst in the foreground!');
        print('Message data: ${message.data}');

        if (message.notification != null) {
            print('Message also contained a notification: ${message.notification}');
        }
    });
}

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
    print('message from background handler');
    print("Handling a background message: ${message.messageId}");
}

我有一种感觉,onMessage().listen(...) 没有订阅,所以什么都不执行。

你有什么建议,我做错了什么?

提前致谢, 乌韦

环境

Firebase 控制台云消息传递

华为 P20 和三星 Galaxy Tab A 10,均在 Android 10 上。

pubspec.yaml

firebase_core: "^1.0.1"
firebase_messaging: "^9.0.0"

android/build.gradle

dependencies {
    ...
    classpath 'com.google.gms:google-services:4.3.5'

android/app/build.gradle

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

android {
    compileSdkVersion 30
    ...
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 30
        ...

AndroidManifest.xml

<activity ...>
    ...
    <intent-filter>
        <action android:name="FLUTTER_NOTIFICATION_CLICK" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</activity>

颤抖的医生

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, 2.0.2, on macOS 11.2.1 20D75 darwin-x64, locale de)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[!] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
    Download at: https://developer.apple.com/xcode/download/
    Or install Xcode via the App Store.
    Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
[✓] Chrome - develop for the web
[✓] Android Studio
[✓] Android Studio (version 4.1)
[✓] IntelliJ IDEA Community Edition (version 2020.1.2)
[✓] VS Code (version 1.53.0)
[✓] Connected device (3 available)

! Doctor found issues in 1 category.

【问题讨论】:

  • 嗨,这个问题解决了吗?

标签: android firebase flutter firebase-cloud-messaging flutter2.0


【解决方案1】:

有一个带有 firebase_messaging 9.0.0 插件的issue

如果您在鼠标悬停在 RemoteMessage 类上时使用 Cmd+Click 或 Ctrl+Click 导航到工厂 RemoteMessage.fromMap() 的定义),则在 return 语句中更改

contentAvailable: map['contentAvailable'], 
mutableContent: map['mutableContent'],

contentAvailable: map['contentAvailable'] ?? false,
to mutableContent: map['mutableContent'] ?? false,

您可能必须确认要修改包。在包更新之前,这应该可以帮助您完成。

【讨论】:

  • 我在 9.1.3 版本中遇到了同样的问题,尽管问题表明这是已修复的。
  • 与 10.0.1 版本有同样的问题
  • 有人有解决方案吗?
【解决方案2】:

我遇到了同样的问题。紧接着

await Firebase.initializeApp();

我添加了这一行:

await FirebaseMessaging.instance.getToken();

在我的 main.dart 中并在谷歌云控制台上启用 Firebase In-App Messaging API

【讨论】:

    猜你喜欢
    • 2018-05-27
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2019-09-09
    • 2020-07-09
    相关资源
    最近更新 更多