Flutter Firebase 消息在应用程序在后台运行或关闭时无法在 IOS 上运行

max*_*ax 26 ios firebase flutter firebase-cloud-messaging

我已经配置了带有 flutter Notification 的 firebase Cloud Messaging 正在前台工作。但在后台运行或应用程序被杀死时不起作用。

完成以下步骤。

还尝试按照Flutter Firebase Cloud Messaging - Notification when app in background 但它仍然无法正常工作中的建议删除以下行。

if (@available(iOS 10.0, *)) { [UNUserNotificationCenter currentNotificationCenter].delegate = (id) self; }

Flutter Doctor

[?] Flutter (Channel beta, v1.12.13+hotfix.6, on Mac OS X 10.14.5 18F132, locale en-IN)

[?] Android toolchain - develop for Android devices

    ? Unable to locate Android SDK.

      Install Android Studio from: https://developer.android.com/studio/index.html

      On first launch it will assist you in installing the Android SDK components.

      (or visit https://flutter.dev/setup/#android-setup for detailed instructions).

      If the Android SDK has been installed to a custom location, set ANDROID_HOME to that location.

      You may also want to add it to your PATH environment variable.



[?] Xcode - develop for iOS and macOS (Xcode 11.3)

[?] Chrome - develop for the web

[!] Android Studio (not installed)

[?] Connected device (3 available)
Run Code Online (Sandbox Code Playgroud)

我注意到的一件事是,当应用程序第一次安装时,它不会要求我检查是否允许应用程序发送推送通知。

我的代码在登录页面后的页面上有以下几行。

_firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, badge: true, alert: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
Run Code Online (Sandbox Code Playgroud)

还交叉检查以下所有设置都已勾选。

在此处输入图片说明

小智 7

确保在下的选项卡Push Notifications & Background Mods中启用了功能,而不仅仅是像我正在做的那样。AllSigning & CapabilitiesProfile

在此输入图像描述

https://github.com/FirebaseExtended/flutterfire/issues/1041#issuecomment-612660797


max*_*ax 7

我的解决方案如下,并且有效。

publspec.yaml

firebase_messaging:^6.0.9

如果使用,从 ios/runner/AppDelegate.m 或 ios/runner/AppDelegate.swift 中删除以下行。

if (@available(iOS 10.0, *)) {
  [UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
 }
Run Code Online (Sandbox Code Playgroud)

在 Dart 代码中使用临时的false,应该弹出来确认通知。

myFirebaseMessagingService.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: false));
Run Code Online (Sandbox Code Playgroud)

在 Xcode 中跟随推送通知以及后台获取和远程通知。

在此处输入图片说明


dcg*_*dcg 7

经过很长时间寻找修复程序,这为我解决了问题:

  1. 在您的 info.plist 中:

     FirebaseAppDelegateProxyEnabled: false
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将此添加到您的 AppDelegate:

    override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
       Messaging.messaging().apnsToken = deviceToken
       super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
     }
    
    Run Code Online (Sandbox Code Playgroud)

  • 不要忘记在 AppDelegate 中添加“import FirebaseMessaging” (3认同)
  • 只有这个方法,你只需要将它放在AppDelegate类中并导入Firebase (2认同)