【问题标题】:Flutter Firebase Messaging Not working on IOS when app running in background or closed当应用程序在后台运行或关闭时,Flutter Firebase Messaging 在 IOS 上不起作用
【发布时间】:2020-06-06 13:55:59
【问题描述】:

我已经配置了带有颤振的 Firebase 云消息传递通知正在前台工作。但在后台运行或应用程序被杀死时无法正常工作。

以下步骤已完成。

还尝试按照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)

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

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

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

还交叉检查是否勾选了所有以下设置。

【问题讨论】:

  • 新版本发布对此有何更新?
  • 我猜你正在使用 local_notification 这个包,这是 local_notification 和这个包之间的常见问题冲突
  • 我花了一个星期,但没有任何效果。

标签: ios firebase flutter firebase-cloud-messaging


【解决方案1】:

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

  1. 在您的 info.plist 中:

     FirebaseAppDelegateProxyEnabled: false
    
  2. 将此添加到您的 AppDelegate:

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

【讨论】:

  • 您在 AppDelegate 中添加了什么可以分享您的代码吗?
  • 只有这个方法,你只需要把它放在AppDelegate类里面并导入Firebase
  • 谢谢@dcg,您的解决方案对我有用。这应该在 firebase_messaging 文档中。
  • AppDelegate 更改的导入是什么?
  • 别忘了在 AppDelegate 中添加“import FirebaseMessaging”
【解决方案2】:

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

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;
 }

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

myFirebaseMessagingService.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: false));

在 Xcode 中跟随 Push 通知以及后台 Fetch 和远程通知。

【讨论】:

    【解决方案3】:

    确保在Signing &amp; Capabilities 下的All 选项卡中启用了Push Notifications &amp; Background Mods 功能,而不仅仅是像我所做的那样在Profile 中启用。

    ScreenShot

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

    【讨论】:

      【解决方案4】:

      我有类似的问题,我需要撤销旧密钥,创建一个新的身份验证密钥并再次上传以使其正常工作:

      https://firebase.google.com/docs/cloud-messaging/ios/certs#create_the_authentication_key https://firebase.google.com/docs/cloud-messaging/ios/client#upload_your_apns_authentication_key

      【讨论】:

        【解决方案5】:

        AppDelegate 和 Info.plist 中无需更改或添加任何内容,只需提供正确的通知负载即可触发:

        样本有效载荷

        {
          "to" : "my_fcm_token",
          "notification" : {
         "body" : "sample body",
         "OrganizationId":"2",
         "content_available" : true,
         "mutable_content": true,
         "priority" : "high",
         "subtitle":"sample sub-title",
         "Title":"hello"
         },
          "data" : {
            "msgBody" : "Body of your Notification",
            "msgTitle": "Title of your Notification",
            "msgId": "000001",
            "data" : "This is sample payloadData"
          }
        }
        

        【讨论】:

          【解决方案6】:

          我设法让我的应用在 Android 上运行并收听 onBackgroundMessage(我刚刚从有效负载中删除了通知对象,只留下了数据对象)。

              final data = {
            "registration_ids": userToken,
            "click_action": "FLUTTER_NOTIFICATION_CLICK",
            "priority": "high",
            "collapse_key": "type_a",
            "data": {
              "click_action": "FLUTTER_NOTIFICATION_CLICK",
              "other_data": "any message",
              "title": 'NewTextTitle',
              "body": 'NewTextBody',
              "priority": "high",
              "click_action": "FLUTTER_NOTIFICATION_CLICK",
            },
          };
          

          【讨论】:

          • 问题是让它在IOS上运行。不是安卓,所以这个答案是无效的
          【解决方案7】:

          就我而言,我解决了如下问题。

          完成教程指南并尝试以下操作:

          • 编辑:iOS/Flutter/AppFrameworkInfo.plist

            • &lt;key&gt;CFBundleIdentifier&lt;/key&gt; &lt;string&gt;{bundleIdentifier}.flutter&lt;/string&gt;
            • {bundleIdentifier} 是: 输入设置为 https://developer.apple.com 的 Bundle 名称。
          • 例子

            • Developer.apple: com.example.app
            • xcode BundleIdentifier : com.example.app
            • iOS/Flutter/AppFrameworkInfo.plist : com.example.app.flutter
          • 测试环境:

            • Firebase_core:^0.4.5
            • Firebase_messaging:^6.0.16
          • (附加)从您的服务器或页面发送时

            • 请设置“通知”项。

          【讨论】:

          • 哪个教程?你能完成它吗?
          猜你喜欢
          • 2020-01-29
          • 1970-01-01
          • 2020-10-06
          • 2021-03-11
          • 2021-11-22
          • 2021-08-29
          • 2017-02-02
          • 1970-01-01
          • 2021-10-22
          相关资源
          最近更新 更多