【问题标题】:Trying out FCM. First steps尝试 FCM。第一步
【发布时间】:2020-09-27 06:39:14
【问题描述】:

我正在试用 Firebase 云消息传递。在这一点上,我已经阅读了一些相关文档,但我没有任何工作。

我已经为此设置了一个 iOS 应用和一个 Firebase 项目。

看着这个GitHub sample,我有个问题。

这行代码是按原样使用,还是占位符,我应该用我的某个 ID 替换 "gcm.message_id"? (如果是,是什么?)

  let gcmMessageIDKey = "gcm.message_id"

更详细的,代码如下:

....
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
  let gcmMessageIDKey = "gcm.message_id"

  func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    FirebaseApp.configure()

    // [START set_messaging_delegate]
    Messaging.messaging().delegate = self
    // [END set_messaging_delegate]

.......

然后gcmMessageIDKey在后面的代码中使用,像这里:

  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.
    // TODO: Handle data of notification
    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.messaging().appDidReceiveMessage(userInfo)
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
      print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)
  }

我想知道,因为我已经尝试在我自己的应用程序中包含一些这样的代码,这似乎是有意义的。然后从 Firebase 控制台 Cloud Messaging 尝试发送测试消息。 但根本没有任何效果。

非常欢迎任何相关的提示,即使是很小的相关提示。

【问题讨论】:

  • 请编辑问题以显示完整的代码示例。随着时间的推移,外部链接可能会发生变化或失效,这将使这个问题在未来对其他人没有帮助。
  • 我刚刚编辑了问题。

标签: ios swift firebase firebase-cloud-messaging


【解决方案1】:

字符串按字面意思理解。它是 userInfo 哈希的键,用于获取传入 FCM 消息 ID 的值。

如果您根本没有收到消息,则说明有其他问题,与此字符串的值无关。

【讨论】:

  • 好的。关于什么可能是错误的,你会先检查什么?知道我之前做了很多阅读和准备工作(设置 Firebase 项目以使其工作,设置 APNs ....)
  • Stack Overflow 上有很多关于诊断发送消息的问题的问题。我建议做一些研究,因为你可能没有遇到任何新的东西。
猜你喜欢
  • 2019-12-15
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 2020-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多