1

我需要一些帮助。

我完全不知道为什么我的通知不起作用。

我的 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”永远不会被调用。我还在我的开发者帐户中创建了“Apple 推送通知服务 SSL 证书”,将“推送通知”添加到我在 Xcode 中的功能中,但它仍然无法正常工作。每次我点击“允许”以允许通知时,它都会显示“访问权限”,但它不会打印出任何设备令牌。“isRegisterForRemoteNotifications”也返回 true。

我也在互联网上搜索了大约 2-3 个小时,但我还没有找到任何解决方案。

我正在使用 Xcode 12 Beta 2 和 iOS 14 Dev Beta 2。

谢谢你。

4

1 回答 1

2

您需要在真机上运行您的代码,您无法在模拟器上获取令牌

于 2020-07-10T21:14:42.683 回答