[英]Not receiving Firebase Cloud Messaging Notifications in ios
我在我ios的app中使用了Firebase Cloud Messaging Notification,但是在真機測試的時候,發現收不到任何推送消息。
我使用 ios 的 firebase 統一包。 我檢查了所有步驟,但我不知道問題出在哪里。
我使用 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
訂閱操作已暫停,因為您沒有令牌。 一旦您獲得 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 有一個發送推送通知的示例,其中包括用於存儲這些令牌的數據庫結構,位於此處: Send Firebase Cloud Messaging notifications for new followers 。
要成功訂閱主題,您必須先請求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")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.