gpt4 book ai didi

ios - 注册推送通知功能不会被调用

转载 作者:行者123 更新时间:2023-11-30 12:54:53 25 4
gpt4 key购买 nike

我正在制作这个需要推送通知的甜蜜应用程序。

但是,当尝试获取 Push token 时,似乎并非所有必需的功能都被执行。

用户确实收到此警报:

enter image description here

当用户点击“确定”时,我单步执行代码,发现并非所有内容都被执行:

override func viewDidLoad(){
let tapper = UITapGestureRecognizer(target: view, action:#selector(UIView.endEditing))
tapper.cancelsTouchesInView = false
view.addGestureRecognizer(tapper)

print("gets called")
registerForPushNotifications(UIApplication.sharedApplication())
}

func registerForPushNotifications(application: UIApplication) {
print("gets called")
let notificationSettings = UIUserNotificationSettings(
forTypes: [.Badge, .Sound, .Alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
print("doesn't get called")
if notificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print("doesn't get called")
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var apnsTokenString = ""

for i in 0..<deviceToken.length {
apnsTokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}

Constant.pushToken = apnsTokenString

print("Device Token:", Constant.pushToken)
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
debugPrint("Error Registering Remote Notification")
}

在我的控制台中打印以下内容:

gets called

gets called

这意味着并非所有必需的函数都被调用。我做错了什么?

最佳答案

假设您在问题中发布的所有代码都在 View Controller 类中,问题是您需要将 UIApplicationDelegate 方法放在实际的应用程序委托(delegate)类中,而不是 View Controller 类中。只需将这些方法移至适当的类,它们就会起作用。

关于ios - 注册推送通知功能不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40541144/

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