我正嘗試使用FCM令牌從Firebase通知控制檯向特定設備發送簡單的推送通知。 Firebase通知控制檯顯示通知已發送,但設備未收到通知。我已嘗試發送通知,然後等待查看控制檯是否從didReceiveRemoteNotification
登錄,但通知耗時過長(小時)顯示爲在Firebase控制檯中發送(即使將優先級設置爲high
)。Firebase通知到設備使用FCM令牌說已發送但未收到
應用代表
import UIKit
import Firebase
import FirebaseStorage
import FirebaseDatabase
import FirebaseMessaging
import CoreData
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Use Firebase library to configure APIs
FirebaseApp.configure()
/////
// For Firebase Cloud Messaging (FCM)
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()
// End of [for Firebase Cloud Messaging (FCM)]
/////
return true
}
///////////////////////
// FCM Setup
// Monitor token generation for FCM: Be notified whenever the FCM token is updated
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
// Monitor token generation for FCM:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
} // Handle messages received through the FCM APNs interface
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
print("didReceiveRemoteNotification")
// 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.
// gcm_message_id
if let messageID = userInfo["gcmMessageIDKey"] {
print("Message ID: \(messageID)")
}
^我的猜測是,這個問題可能與「gcm_message_id」 /「gcmMessageId」 /「gcm.message_id」做,因爲它在每個不同三種方法我試過下面
// Print full message.
print(userInfo)
}
// Handle messages received through the FCM APNs interface
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("didReceiveRemoteNotification (withCompletionHandeler)")
// 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)")
}
^我的猜測是,這個問題可能與「gcm_message_id」 /「gcmMessageId」 /「gcm.message_id」做,因爲它是不同的我n各自的三種方法我試過下面
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
// End of [FCM Setup]
///////////////////////
}
視圖控制器
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Retrieve the current registration token for Firebase Cloud Messaging (FCM)
let token = Messaging.messaging().fcmToken
print("FCM token: \(token ?? "")")
}
}
應享權利&啓用推送通知
我已經加入了推送權利和啓用後臺模式推送通知和adde d將GoogleService-Info.plist添加到我的項目中。
發送通知我創建從火力地堡通知控制檯通知(如下圖所示),所以應該沒有問題與通知本身的結構
我曾嘗試以下方法來解決這個問題,但都產生相同的結果:
有誰知道爲什麼通知被標記爲已發送Firebase通知控制檯但未顯示在設備上?
請檢查以下內容:1.您是否添加了推送權利? 2.你添加了GoogleService-Info.plist嗎? –
@ jn-se,感謝您的評論。我添加了推送權利(併爲推送通知啓用了後臺模式),並將GoogleService-Info.plist添加到了我的項目中。我已將屏幕截圖添加到上述問題 – Rbar
除權利外:1。 「但通知需要很長時間才能顯示爲在Firebase控制檯中發送(即使我將優先級設置爲高)。」< - 首先解決此問題。它不應該是這樣的。 2.確保你沒有收到註冊令牌的任何錯誤3.然後編輯你的問題,並添加有效載荷的確切**結構**以及它的外觀。很多時候,有效載荷的結構是錯誤的,因此你什麼也得不到! – Honey