添加Firebase SDK后,iOS推送通知停止工作

时间:2018-05-22 00:14:35

标签: ios swift firebase firebase-cloud-messaging

我在使用APNS的项目中使用推送通知。 推送通知在添加和配置Firebase SDK之前运行良好。 我注意到在添加Firebase后,函数:didRegisterForRemoteNotificationsWithDeviceToken未被调用。

我不知道在Firebase中是否有必须禁用的内容,如果我删除了Firebase设置(FirebaseApp.configure()),则会再次调用函数didRegisterForRemoteNotificationsWithDeviceToken

我的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // ... existing code ...
    registerForPushNotifications()
    return true
}

func registerForPushNotifications() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
        (granted, error) in
        print("Permission granted: \(granted)")

        guard granted else { return }
    }
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let tokenParts = deviceToken.map { data -> String in
        return String(format: "%02.2hhx", data)
    }

    let token = tokenParts.joined()
    print("Device Token: \(token)")
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Failed to register: \(error)")
}

1 个答案:

答案 0 :(得分:1)