簡體   English   中英

Flutter 2.0 和 Firebase 雲消息:在 Android 上未調用 onMessage

[英]Flutter 2.0 with Firebase Cloud Messaging: onMessage not called on Android

我在 Flutter 2.0 中遇到了 Firebase 雲消息傳遞 onMessage 的問題。

function

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"

安卓/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>

flutter博士

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.

firebase_messaging 9.0.0 插件存在問題

如果您使用 Cmd+Click 或 Ctrl+Click 導航到工廠 RemoteMessage.fromMap() 的定義,同時將鼠標懸停在 RemoteMessage class 上),在返回語句中,更改

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

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

您可能需要確認是否要修改 package。 在 package 更新之前,這應該可以幫助您完成。

我有這個完全相同的問題。 緊接着

await Firebase.initializeApp();

我添加了這一行:

await FirebaseMessaging.instance.getToken();

在我的 main.dart 並在谷歌雲控制台上啟用 Firebase 應用內消息 API

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM