我使用Firebase作为后端 . 我还使用FCM作为Firebase提供的功能之一 . FCM在iOS 10中运行良好,但在切换到iOS 11后,推送通知停止进入用户设备,我自己没有收到任何从 Cloud 功能或Firebase控制台中的通知部分发送的推送通知 . 如何解决这个问题?
Update: 我从Firebase Notifcations发送了几个推送通知,但他们没有来 .
// MARK: - Push notification
extension AppDelegate: UNUserNotificationCenterDelegate {
func registerPushNotification(_ application: UIApplication) {
// 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 })
// For iOS 10 data message (sent via FCM)
Messaging.messaging().delegate = self
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
//When the notifications of this code worked well, there was not yet.
Messaging.messaging().apnsToken = deviceToken
}
// [START receive_message]
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
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
debugPrint("Message ID: \(messageID)")
}
// Print full message.
debugPrint(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// 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
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
debugPrint(userInfo)
completionHandler(.newData)
}
// showing push notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if let userInfo = response.notification.request.content.userInfo as? [String : Any] {
let routerManager = RouterManager()
routerManager.launchRouting(userInfo)
}
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if let userInfo = notification.request.content.userInfo as? [String : Any] {
if let categoryID = userInfo["categoryID"] as? String {
if categoryID == RouterManager.Categories.newMessage.id {
if let currentConversation = ChatGeneralManager.shared.currentChatPersonalConversation, let dataID = userInfo["dataID"] as? String {
// dataID is conversationd id for newMessage
if currentConversation.id == dataID {
completionHandler([])
return
}
}
}
}
if let badge = notification.request.content.badge {
AppBadgesManager.shared.pushNotificationHandler(userInfo, pushNotificationBadgeNumber: badge.intValue)
}
}
completionHandler([.alert,.sound, .badge])
}
}
// [START ios_10_data_message_handling]
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
let pushNotificationManager = PushNotificationManager()
pushNotificationManager.saveNotificationTokenInDatabase(token: fcmToken, success: nil, fail: nil)
}
// Receive data message on iOS 10 devices while app is in the foreground.
func application(received remoteMessage: MessagingRemoteMessage) {
debugPrint(remoteMessage.appData)
}
}
4 回答
看来,使用FirebaseInstanceID 2.0.3推送通知不起作用 . 它帮助我设置:pod'FirebaseInstanceID',“2.0.0” . 也许在下一个版本中,这将被修复 .
请更新您的FCM广告连播
到最新版本,即2.0.6
它应该适用于iOS 11设备 .
我读了这个topic并将此代码
application.registerForRemoteNotifications()
添加到AppDelegate方法didFinishLaunchingWithOptions
并且它有效,但不幸的是,当前用户在更新到新版本之前不会收到通知 . 请参阅https://github.com/firebase/quickstart-ios/issues/327#issuecomment-331782299这项工作对我来说 . IOS 11,SWIFT 4.取自:https://medium.com/ios-os-x-development/ios-remote-notification-with-firebase-tutorial-118acd3ebce1
PODS pod'Firebase / Core'pod'Firebase / Analytics'pod'Firebase / Auth'pod'Firebase / Crash'pod'Firebase / Database'pod'Firebase / DynamicLinks'pod'Firebase / Invites'pod'Firebase / Messaging'pod “Firebase / Performance”广告连播“Firebase / RemoteConfig”广告连播“Firebase / Storage”广告连播“FirebaseInstanceID”