【问题标题】:FCM data message not coming when app is closed in ios当应用程序在 ios 中关闭时没有出现 FCM 数据消息
【发布时间】:2021-10-30 14:25:02
【问题描述】:

我已将 FCM 与 flutter_local_notification 集成到我的颤振项目中。在 Android 中一切正常。但是,ios 也几乎可以工作,除非应用程序从历史记录中关闭。当应用程序从历史记录中清除时,消息不会触发。我在推送通知中只使用数据消息。请看下面:

{
   "to": "/topics/global",
   "priority": "high",
   "content_available": true,
   "data": {
      "title": "Demo Title",
      "message": "Demo Message",
      "payload": "basic"
   }
}

如您所见,json 中没有 notification 对象。此配置在 Android(前台和后台)和 ios 仅前台上运行良好。但是,如果我在这个 json 中添加 notification 对象,令人惊讶的是 it works even the app is closed

但由于我需要手动处理通知,该选项不适合我的用例。所以我进一步研究并禁用了Method Swizzling

<key>FirebaseAppDelegateProxyEnabled</key> 
<false/>

但这也无济于事。现在我有点迷路了,找不到达到预期结果的方法。我的AppDelegate.swift 如下所示

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
   override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
    }
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

注意:ios 应用在 TestFlight 中发布并在真实设备上进行测试

【问题讨论】:

  • 你注册后台处理程序了吗?
  • 是的,我有@Siddharthjha
  • 如果有帮助,我来自世博会背景,但他们倾向于在本机设置或基本上归结为这一点的事情中提供很好的集成提示。尝试阅读此文档docs.expo.dev/versions/latest/sdk/notifications,看看是否有任何新的启发您。请查看“当应用不在前台时处理传入通知”部分

标签: ios firebase flutter push-notification firebase-cloud-messaging


【解决方案1】:

经过长时间的研究,我找不到解决此问题的绝对方法。但是,我已经针对该问题实施了解决方法。

我有 3 种发送通知的场景

1. Sending notification to specific user
2. Sending notification to all user
3. Sending notification to a topic

当用户打开应用程序时,我会检查用户正在使用哪个平台。如果是 Android,那么我将设备令牌存储为 android,否则为 ios。同时,如果平台是Android,我还将用户注册到一个主题global_android,如果平台是ios,则为global_ios

现在,当我从管理面板发送通知时,我将条件与上述 3 种情况相匹配。对于 android,我使用以下 json 配置发送通知

{
  "to": "topic || token",
  "priority": "high",
  "content_available": true,
  "data": {
     "title": "Demo Title",
     "message": "Demo Message",
     "payload": "basic"
  }
}

对于ios,我使用以下配置

{
  "to": "/topics/global",
  "priority": "high",
  "content_available": true,
  "notification": {
     "title": "Demo Title",
     "body": "Demo Message"
  },
  "data":{
      "key1":"value",
      "key2":"value"
  }
}

现在,当我收到通知时,我会再次检查平台。如果是Android,那么我会使用“flutter_local_notification”插件显示自定义通知,如果是 ios,那么我什么也不做,因为 firebase 已经显示了通知。

这不是一个绝对的解决方案,但猜猜看,它对我有用。

【讨论】:

    猜你喜欢
    • 2016-11-13
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多