【问题标题】:FCM Push notification not working in iOS 11FCM 推送通知在 iOS 11 中不起作用
【发布时间】:2018-04-24 08:46:58
【问题描述】:

我需要处理推送通知,这是使用较低版本的 ios 完成的,但在 ios 11 中从未收到任何推送通知。我使用 Firebase 云消息传递。拜托,谁有解决方案,请分享。

【问题讨论】:

    标签: ios swift firebase apple-push-notifications


    【解决方案1】:

    我遇到了同样的问题,问题是我在 firebase 控制台中缺少我的 APN 配置。

    【讨论】:

    • 您好,您不能在答案内提问。如果您遇到同样的问题,请检查答案,否则请使用您的配置创建新问题。
    【解决方案2】:

    问题似乎出在

    • FirebaseInstanceID 版本低于 1.0.9

    • FirebaseInstanceID 2.0.1 - 2.0.3 之间的版本

    如下设置你的 pod 文件:

    对于 Swift 2.3 和 Xcode 8:(已安装 FirebaseInstanceID v1.1.0)

    pod 'Firebase/Core', '3.8.0'
    pod 'Firebase/Messaging'
    

    对于 Swift 3 和 Xcode 9:

    pod 'Firebase/Core'
    pod 'Firebase/Messaging'
    pod 'FirebaseInstanceID', "2.0.0
    

    我不想升级到 FirebaseInstanceID 2.0.0 来解决这个问题,因为我只想使用 Swift 2.3

    【讨论】:

      【解决方案3】:

      请检查为

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
          // Use Firebase library to configure APIs
          FirebaseApp.configure()
          self.registerForPushNotifications(application: application)
          Messaging.messaging().delegate = self
      
          if let token = InstanceID.instanceID().token() {
              NSLog("FCM TOKEN : \(token)")
              DataModel.sharedInstance.onSetUserFCMStringToken(FCM: token)
              self.connectToFcm()
          }
          if launchOptions != nil {
              //opened from a push notification when the app is closed
              _ = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] ?? [AnyHashable: Any]()
          }
          else {
              //opened app without a push notification.
          }
          return true
      }
      

      @available(iOS 10, *)

      extension AppDelegate: UNUserNotificationCenterDelegate {
      // iOS10+, called when presenting notification in foreground
      func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
          let userInfo = notification.request.content.userInfo
          NSLog("[UserNotificationCenter] willPresentNotification: \(userInfo)")
          //TODO: Handle foreground notification
          completionHandler([.alert])
      }
      
      // iOS10+, called when received response (default open, dismiss or custom action) for a notification
      func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
          let userInfo = response.notification.request.content.userInfo
          NSLog("[UserNotificationCenter] didReceiveResponse: \(userInfo)")
          //TODO: Handle background notification
          completionHandler()
      }}
      
      extension AppDelegate : MessagingDelegate {
      //MARK: FCM Token Refreshed
      func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
          NSLog("[RemoteNotification] didRefreshRegistrationToken: \(fcmToken)")
      }
      
      // Receive data message on iOS 10 devices while app is in the foreground.
      func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
          NSLog("remoteMessage: \(remoteMessage.appData)")
      }}
      
      //Register for push notification.
      func registerForPushNotifications(application: UIApplication) {
          if #available(iOS 10.0, *) {
              let center  = UNUserNotificationCenter.current()
              center.delegate = self
              center.requestAuthorization(options: [.alert,.sound]) { (granted, error) in
                  if error == nil{
                      DispatchQueue.main.async(execute: {
                          application.registerForRemoteNotifications()
                      })
                  }
              }
          }
          else {
      
              let settings = UIUserNotificationSettings(types: [.alert,.sound], categories: nil)
              application.registerUserNotificationSettings(settings)
              application.registerForRemoteNotifications()
          }
      
          // Add observer for InstanceID token refresh callback.
          NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
      
      }
      
      @objc func tokenRefreshNotification(_ notification: Notification) {
          print(#function)
          if let refreshedToken = InstanceID.instanceID().token() {
              NSLog("Notification: refresh token from FCM -> \(refreshedToken)")
      
          }
          // Connect to FCM since connection may have failed when attempted before having a token.
          connectToFcm()
      }
      
      func connectToFcm() {
          // Won't connect since there is no token
          guard InstanceID.instanceID().token() != nil else {
              NSLog("FCM: Token does not exist.")
              return
          }
      
          Messaging.messaging().shouldEstablishDirectChannel = true
      }
      
      func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
          NSLog("Notification: Unable to register for remote notifications: \(error.localizedDescription)")
      }
      
      
      // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
      // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to the InstanceID token.
      func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
      
      
          Messaging.messaging().apnsToken = deviceToken
      
      
      }
      
      // iOS9, called when presenting notification in foreground
      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
          NSLog("didReceiveRemoteNotification for iOS9: \(userInfo)")
      
      }
      
      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
      
      
      
      
      }
      

      【讨论】:

      • 嘿嘿,我用的是FirebaseInstanceID', "2.0.0" 甚至是最新的pod,但是没有收到通知,你能指导我,如何解决这个问题
      • 经过大量搜索后,我只使用了最新版本或默认版本的 pod,然后检查了在我这边工作的情况,存在证书问题或有时 FCM 令牌问题请检查
      • 我正在使用的 Pem 文件正在其他在线门户中工作。我已在 FCM 中添加,但此处显示消息已发送,但我的设备上尚未收到。我现在面临的问题是
      • 您是通过 fcm 令牌还是其他方式发送通知?
      • 是的,我正在使用它。你能给我你的 Skype ID 吗?或通过 vijayvir.smartitventures@gmail.com 给我发消息
      猜你喜欢
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 2021-05-04
      相关资源
      最近更新 更多