Flutter Firebase Messaging在iOS后台或关闭应用时无法工作

42

我已经在flutter中配置了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");
    });

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

输入图像描述


这个新版本发布后有任何更新吗? - LOG_TAG
我猜你正在使用这个包的local_notification功能,这是local_notification和这个包之间常见的冲突问题。 - Mahmoud Niypoo
我花了一周的时间,但结果仍然是一无所获。 - Gennadii Ianchev
你是否曾经得到过解决方案? - Ruder Buster
9个回答

17

经过长时间的寻找,以下方法为我解决了这个问题:

  1. 在你的 info.plist 文件中:

  2.  FirebaseAppDelegateProxyEnabled: false
    
  3. 将以下代码添加到你的AppDelegate中:

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

2
只需将此方法放置在AppDelegate类中并导入Firebase即可。 - dcg
AppDelegate 的更改需要导入哪些内容? - yardstick17
6
请不要忘记在AppDelegate中添加 "import FirebaseMessaging"。 - Muhammad Adil
已经有一个名为“application”的函数了,我应该复制函数体内的两行代码吗? - Ahmed Nabil
1
请分享您的AppDelegate.swift完整代码,因为它在我的设备上无法工作,并显示“无法在范围内找到'deviceToken'”错误。 - Muhammad Umair Saqib
显示剩余5条评论

11

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

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的provisional属性,应该会弹出通知确认框。

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

在Xcode中,可以通过后台获取和远程通知来实现推送通知。

enter image description here


7

3

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

2

0
在我的情况下,Firebase 项目设置中来自苹果开发者账户的 iOS 应用程序的 Team ID 是不正确的。

0

在我的情况下,我是这样解决的。

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

  • 编辑:iOS/Flutter/AppFrameworkInfo.plist

    • <key>CFBundleIdentifier</key> <string>{bundleIdentifier}.flutter</string>
    • {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
  • (附加)从您的服务器或页面发送时

    • 请设置“notification”项。

哪个教程?你能完成它吗? - M. A.

0

我成功让我的应用程序在 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",
  },
};

10
问题是让它在IOS上运行,而不是安卓,因此这个答案是无效的。 - Gino

0
在花了几天的时间后,我终于找到了一个解决方案。请仔细按照以下步骤操作:
1. 将以下代码添加到你的 `info.plist` 文件中:`FirebaseAppDelegateProxyEnabled : NO`

将以下代码添加到AppDelegate中。

2

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

为了在iOS中激活后台处理功能,需要在通知请求中包含以下字段: "content_available": true 因此,无法通过Firebase控制台测试此功能。要实现这一点,必须使用Postman发送通知。
将以下参数添加到请求的headers中: Content-Type: application/json Authorization: key = your_server_key

enter image description here

通过邮差发送通知

请求的URL:
https://fcm.googleapis.com/fcm/send

请求体:
{
  "to" : "your_fcm_token",
  "notification" : {
 "body" : "sample body",
 "content_available" : true,
 "priority" : "high",
 "Title":"hello"
 }
}

这对我来说很有效!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接