didRegisterForRemoteNotificationsWithDeviceToken никогда не вызывается - PullRequest
0 голосов
/ 10 июля 2020

Мне нужна помощь.

Я совершенно не понимаю, почему мои уведомления не работают.

Мой AppDelegate выглядит так:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print("token: \(token)")
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("failed to register for remote notifications with with error: \(error)")
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
    
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            print("granted: (\(granted)")
        }
    
        UIApplication.shared.registerForRemoteNotifications()
    
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
}

Проблема в , функция didRegisterForRemoteNotificationsWithDeviceToken никогда не вызывается. Я также создал «SSL-сертификаты Apple Pu sh Notification service» в своей учетной записи разработчика, добавил «Pu sh Notifications» к своим возможностям в Xcode, но он все еще не работает. Каждый раз, когда я нажимаю «Разрешить», чтобы разрешить уведомления, он говорит «доступ предоставлен», но не распечатывает токен устройства. isRegisterForRemoteNotifications также возвращает true.

Я также искал в inte rnet около 2-3 часов, но пока не смог найти никакого решения.

Я использую Xcode 12 Beta 2 и iOS 14 Dev Beta 2.

Спасибо.

1 Ответ

1 голос
/ 11 июля 2020

Вам нужно запустить свой код на реальном устройстве, вы не можете получить токен на симуляторе

...