首页 > 解决方案 > iOS 推送通知在添加 Firebase SDK 后停止工作

问题描述

我在我的项目中使用 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)")
}

标签: iosswiftfirebasefirebase-cloud-messaging

解决方案



推荐阅读