gpt4 book ai didi

ios - 从未调用过'didRegisterForRemoteNotificationsWithDeviceToken'

转载 作者:行者123 更新时间:2023-12-01 19:30:56 31 4
gpt4 key购买 nike

我需要协助。
我完全不知道为什么我的通知不起作用。
我的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 Push Notification服务SSL证书”,并在Xcode的功能中添加了“Push Notifications”,但仍然无法使用。每次我在“允许”选项卡上单击以允许通知时,都会说“已授予访问权限”,但不会打印出任何设备 token 。 “isRegisterForRemoteNotifications”也返回true。
我还在互联网上搜索了大约2-3个小时,但找不到任何解决方案。
我正在使用Xcode 12 Beta 2和iOS 14 Dev Beta 2。
谢谢。

最佳答案

您需要在真实设备上运行代码,而无法在模拟器上获取 token

关于ios - 从未调用过'didRegisterForRemoteNotificationsWithDeviceToken',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62842173/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com