簡體   English   中英

Flutter FCM無法運行onBackgroundMessage,未點擊時無法在本地保存通知

[英]Flutter FCM can't run onBackgroundMessage, can't save notifications locally when not clicked

我正在使用 flutter 制作 android 應用程序,其中一項功能是顯示和保存來自 Firebase 的推送通知。

我的問題是如何在不點擊通知的情況下打開應用程序時保存推送通知。

我實現了 fcm,我可以在應用程序運行( onMessage )/在后台( onResume )/終止( onLaunch )時收到通知,

我可以使用上述函數將它們本地保存到存儲庫中,但是這些函數僅在用戶單擊通知時調用,而不是在應用程序使用其圖標啟動時調用。

我試圖保存onBackgroundMessage但它從未被調用,下面是測試 function 試圖在 onBackgroundMessage 時打印一些東西,它在應用程序 class 之外:

Future<void> testing(Map<String, dynamic> message) {
  print("BACKGROUND MSG CALLED");
  return Future.value(null);
}

下面是我在 initState() 中運行的代碼,

附言。 _showNotification 也將調用 saveNotifcation() 效果很好。

firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> msg) async {
    print("onMessage called $msg");
    await _showNotification(msg);
  },
   onBackgroundMessage:
       Platform.isAndroid ? testing : null,
  onLaunch: (Map<String, dynamic> msg) async {
    // give time for splashscreen to run and finish before it's done.
    SchedulerBinding.instance
        .addPostFrameCallback((_) => _showNotification(msg));
  },
  onResume: (Map<String, dynamic> msg) async {
    print("onResume called $msg");
    await _showNotification(msg);
  },
);
firebaseMessaging.requestNotificationPermissions(
    const IosNotificationSettings(
        sound: true, badge: true, alert: true, provisional: true));
firebaseMessaging.onIosSettingsRegistered
    .listen((IosNotificationSettings settings) {
  print("ios registered");
});
firebaseMessaging.getToken().then((){ print("printing Token: {$token}");});

我搜索了很多,但找不到解決方案。 任何幫助都感激不盡。

我正在使用 firebase_messaging: ^7.0.0

Flutter 博士-v:

    [✓] Flutter (Channel stable, 1.22.0, on Mac OS X 10.15.1 19B88, locale en)
    • Flutter version 1.22.0 at /Users/mine/flutter
    • Framework revision d408d302e2 (9 days ago), 2020-09-29 11:49:17 -0700
    • Engine revision 5babba6c4d
    • Dart version 2.10.0
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/mine/Library/Android/sdk
    • Platform android-29, build-tools 29.0.3
    • ANDROID_HOME = /Users/mine/Library/Android/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.3.1, Build version 11C504
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 46.0.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.49.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.15.0

[✓] Connected device (1 available)
    • Mi 9T Pro (mobile) • 83b222f7 • android-arm64 • Android 10 (API 29)

• No issues found!

onBackgroundMessage僅在應用程序處於后台時消息上的通知正文為空時調用。 更多細節在文檔中討論。

暫無
暫無

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

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