Flutter FCM无法在BackgroundMessage上运行,未单击时无法在本地保存通知

时间:2020-10-08 09:36:31

标签: android flutter firebase-cloud-messaging

我正在使用flutter制作一个android应用程序,其中一项功能是显示并保存Firebase的推送通知。

我的问题是打开应用程序时如何保存推送通知而不点击通知。

我实现了fcm,当应用程序运行(onMessage)/后台(onResume)/终止(onLaunch)时,我可以收到通知,

我可以使用上述功能将它们本地保存到存储库中,但是只有在用户单击通知时才会调用它们,而在使用其图标启动应用程序时则不会调用它们。

我试图保存在onBackgroundMessage中,但从未被调用过,下面是测试函数尝试在onBackgroundMessage上打印某些内容,它在App类之外:

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

下面是我在initState()中运行的代码,

ps。 _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 Doctor -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!

0 个答案:

没有答案