在iOS设备上无法接收Firebase云消息通知

4

我在我的iOS应用程序中使用Firebase Cloud Messaging通知,但是当我测试真机时,发现无法接收任何推送消息。

我为iOS使用Firebase Unity包。 我检查了所有步骤,但不知道问题出在哪里。

我使用p8 APNs文件,在钥匙串中使用带有推送通知的开发者标识。

在Xcode中,我打开了选项(推送通知和后台模式),但它无法接收前台或后台的推送消息。

Xcode控制台的日志如下:

[Firebase/Messaging][I-FCM002010] The subscription operation is suspended because you don't have a token. The operation will resume once you get an FCM token.

[GULReachability][I-REA902004] Network status has changed. Code:2, status:Connected


FCM: Initialize Firebase Messaging
FCM: Initialize Firebase Messaging

FCM: Retrieve registration token
FCM: Retrieve registration token

RequestPermissionAsync completed

[Firebase/InstanceID][I-IID003012] Provisioning profile has specifically provisioned devices, most likely a Dev profile.
[Firebase/InstanceID][I-IID003013] APNS Environment in profile: development

你在AppDelegate中设置Firebase推送通知代码了吗? - TheTiger
我使用 Firebase 用于 Unity 软件包,只需将以下内容设置为 Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived; Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived; - zanglengyu
2个回答

3

订阅操作已暂停,因为您没有令牌。一旦获得FCM令牌,操作将恢复。

您需要注册iOS客户端的FCM令牌。

Firebase有非常详细的文档来设置这个:在iOS上设置Firebase云消息传递客户端应用程序

其中关键部分是在您的AppDelegate中包含以下代码:

if #available(iOS 10.0, *) {
  // For iOS 10 display notification (sent via APNS)
  UNUserNotificationCenter.current().delegate = self

  let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
  UNUserNotificationCenter.current().requestAuthorization(
    options: authOptions,
    completionHandler: {_, _ in })
} else {
  let settings: UIUserNotificationSettings =
  UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  application.registerUserNotificationSettings(settings)
}

application.registerForRemoteNotifications()

然后你需要处理所给的令牌。你应该将此令牌存储在Firebase数据库中,然后在发送推送通知时需要包含它。

Firebase有一个发送推送通知的示例,其中包括用于存储这些令牌的数据库结构,位于此处:为新关注者发送Firebase Cloud Messaging通知


[Firebase/InstanceID][I-IID003009] 获取默认令牌失败,错误域=NSURLErrorDomain,错误代码=-1005,“网络连接已丢失。” 新的日志如下... - zanglengyu
1
我已经设置好了所有内容,我的通知似乎也在工作,但我仍然收到那个警告。 - Alejandro Cotilla

3
要成功订阅主题,您需要先请求instanceID
FirebaseApp.configure()

UNUserNotificationCenter.current().delegate = self

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in })

application.registerForRemoteNotifications()

InstanceID.instanceID().instanceID { (result, _) in
    if result != nil {
        // Receive notifications from the "all" topic
        Messaging.messaging().subscribe(toTopic: "all")
    }
}

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