gpt4 book ai didi

swift - didRegisterForRemoteNotificationsWithDeviceToken 不会被触发 - 推送通知不起作用

转载 作者:行者123 更新时间:2023-12-04 02:37:11 25 4
gpt4 key购买 nike

我已经花了几个小时试图找出为什么 didRegisterForRemoteNotificationsWithDeviceToken 没有被调用。它以前有效。我几个星期没碰它了。现在停止工作了。

这是我的设置:

  1. didRegisterForRemoteNotificationsWithDeviceToken 坐在 SceneDelegate
  2. 已将 SceneDelegate 声明为 UNUserNotificationCenterDelegate
  3. 我在 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) 中设置 UNUserNotificationCenter.current().delegate = self >
  4. 我正在从我的应用程序中的一个 UIViewController 调用 UNUserNotificationCenter.current().requestAuthorization { granted, error in...,特别是在 viewDidLoad 中 Controller - 我收到授权弹出窗口,接受时收到 true 返回 granted
  5. UNUserNotificationCenter.current().getNotificationSettings { settings in ... 中检查 settings.authorizationStatus == .authorized 返回 true
  6. 推送通知已作为应用程序的一项功能添加到“签名和功能”中,并且已创建授权文件。我什至已经删除并重新创建了授权文件 - 只是为了确定...
  7. 我已确保在真实设备上运行该应用程序(通过 Xcode 和 TestFlight)- 因此未调用的委托(delegate)与在模拟器中运行的应用程序无关。
  8. 已检查是否至少调用了 didFailToRegisterForRemoteNotificationsWithError - 但它没有。
  9. 我已经尝试使用我以前从未用于测试的第二个物理设备。
  10. 我什至在这里检查了 APNS 沙盒的状态:https://developer.apple.com/system-status/
  11. 如果我通过终端命令 xcrun simctl push... 向模拟器发送测试通知并且我收到通知,则会调用 didReceive 委托(delegate)。
  12. 供应配置文件由 Xcode 管理,指定的 AppID 为推送通知配置。证书在苹果开发者账户中设置。

以下是我在我的应用程序 UIViewControlles 之一的 viewDidLoad 中请求用户授权的方式

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
DispatchQueue.main.async {
if (granted) {
//self.processInitialAPNSRegistration()
UIApplication.shared.registerForRemoteNotifications()
UserDefaults.standard.set(true, forKey: "pushNotificationsEnabled")
}
print("permission granted?: \(granted)")
}
}

这里是位于 SceneDelegate 中的 didRegisterForRemoteNotificationsWithDeviceToken 委托(delegate)方法。注意:我还在 scene willConnectTo 中设置了 UNUserNotificationCenter.current().delegate = self 并声明了SceneDelegate以实现 UNUserNotificationCenterDelegate` 协议(protocol)。

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

self.apiService.setAPNSToken(apnsToken: token, completion: {result in
switch result {
case .success(let resultString):
DispatchQueue.main.async {
UserDefaults.standard.set(token, forKey: "apnsToken")
print(resultString, " TOKEN IS: \(token)")
}
case .failure(let error):
print("AN ERROR OCCURED: \(error.localizedDescription)")
}
})

}

无论我做什么,didRegisterForRemoteNotificationsWithDeviceToken 都不会被触发。我对出了什么问题的想法已经用完了。

错在哪里?如何让 didRegisterForRemoteNotificationsWithDeviceToken 再次执行?

最佳答案

我认为你在第二步 didRegisterForRemoteNotificationsWithDeviceToken 中出错了,didFailToRegisterForRemoteNotificationsWithError delegates are available in UIApplicationDelegate notUNUserNotificationCenterDelegate

UIApplicationDelegate 添加到您的 SceneDelegate 类,并在 willConnectTo 函数中将委托(delegate)设置为:

UIApplication.shared.delegate = self

我希望这能奏效...如果您仍然发现问题,请告诉我。

关于swift - didRegisterForRemoteNotificationsWithDeviceToken 不会被触发 - 推送通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61143497/

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