Firebase push notifications not working with iOS

情到浓时终转凉″ 提交于 2020-03-03 06:01:21

问题


I want to implement push notification using Firebase Cloud Messaging

I have setup my project and uploaded APN certificate as explained and I am sending Test messages using fcmtoken to my real device

my configuration is as follows in 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.
    }

it is supposed to see notofication arrivied but it didn't Also set a beak point It seems this part is never being excused thus message is not coming


回答1:


I don’t see this in your AppDelegate unless you have Swizzling enabled

func application(application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
      Messaging.messaging().apnsToken = deviceToken
    }

This code maps your APNs device token to FCM token, which is necessary because APNs token is the only way you can send a push notification.



来源:https://stackoverflow.com/questions/60096175/firebase-push-notifications-not-working-with-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!