gpt4 book ai didi

ios - 没有获取推送通知的 token

转载 作者:行者123 更新时间:2023-11-30 11:32:48 24 4
gpt4 key购买 nike

我正在尝试让推送通知正常工作,但我的印象是从未调用过“didRegisterForRemoteNotificationsWithDeviceToken”方法,“didFailToRegisterForRemoteNotificationsWithError”的情况也是如此。 stackoverflow 上的一些旧线程显示了完全相同的问题,但提供的解决方案对我不起作用:/

我到处搜索过,似乎我遇到了这个问题, token 永远不会打印到控制台,我得到的唯一输出是:

Permission granted: true
Notification settings: <UNNotificationSettings: 0x1c009cb10; authorizationStatus: Authorized, notificationCenterSetting: Enabled, soundSetting: Enabled, badgeSetting: Enabled, lockScreenSetting: Enabled, carPlaySetting: NotSupported, alertSetting: Enabled, alertStyle: Banner>

我做了什么:

  • 检查服务是否已启动:https://developer.apple.com/system-status/
  • 已成功为我的应用颁发证书(APNs 开发)
  • 在不同设备上运行应用
  • 导入的UserNotifications.framework
  • 将推送通知和远程通知(后台模式)功能设置为开启

这是我的代码(appdelegate.swift):

//
// AppDelegate.swift


import UserNotifications
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
registerForPushNotifications()
return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

func registerForPushNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")

guard granted else { return }
self.getNotificationSettings()
}
}

func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
}
}

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)")
}


}

提前致谢!

最佳答案

您从未真正尝试注册推送通知,因此从未调用相关函数。您需要调用 UIApplication.shared.registerForRemoteNotifications() 才能接收推送通知,而您当前的代码仅请求通知权限。

func registerForPushNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}

关于ios - 没有获取推送通知的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50080853/

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