我想使用 Firebase Cloud Messaging 实现推送通知
我已经按照说明设置了我的项目并上传了 APN 证书
我正在使用发送测试消息fcmtoken
到我的真实设备
我在AppDelegate中的配置如下
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
registerForPushNotifications(app: application)
return true
}
func registerForPushNotifications(app: UIApplication) {
UNUserNotificationCenter.current().delegate = self
Messaging.messaging().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (authorized, error) in
if let error = error {
print(error.localizedDescription)
return
}
if authorized {
print("authorized")
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
} else {
print("denied")
}
}
app.registerForRemoteNotifications()
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
func application(_ application: UIApplication,
didReceiveRemoteNotification notification: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("notofication arrivied")
if Auth.auth().canHandleNotification(notification) {
completionHandler(.noData)
return
}
// This notification is not auth related, developer should handle it.
}
它应该看到notofication arrivied
但它并没有设置一个喙点,这部分似乎永远不会被原谅,因此消息不会传来
除非您启用了 Swizzling,否则我在您的 AppDelegate 中看不到此内容
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken
}
此代码将您的 APNs 设备令牌映射到 FCM 令牌,这是必要的,因为 APNs 令牌是您发送推送通知的唯一方式。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)