ios - SwiftUI 应用程序生命周期中的远程推送通知不起作用
问题描述
将出现通知权限请求,然后我将按允许和所有这些,但没有任何委托功能(didFailToRegisterForRemoteNotificationsWithError 或 didRegisterForRemoteNotificationsWithDeviceToken 工作,我正在真实设备上测试......)。我已在 Signing & Capabilities 中启用推送通知,并已通过 Apple Developer 注册证书并将其保存到我的钥匙串中。任何帮助,将不胜感激!
@main
struct Sparrow_NavigationApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
RootView {
ContentView()
}
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current()
.requestAuthorization(
options: [.alert, .sound, .badge]) { granted, _ in
print("Permission granted: \(granted)")
guard granted else { return }
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
guard let aps = userInfo["aps"] as? [String: AnyObject] else {
completionHandler(.failed)
return
}
print("got something, aka the \(aps)")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("device token")
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
print("Device Token: \(token)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Device Token not found.")
}
}
解决方案
推荐阅读
- python - 如何在python中匹配人脸
- ios - “[UILabel]”类型的值没有成员“numberOfLines”
- python - Python 美汤 find_all
- reactjs - 在组件属性值更改后运行 componentDidMount
- npm - 寻找在某个根目录下的每个文件夹中执行“npm run build”的标准方法
- apache-spark - 无法在纱线集群模式下使用 spark 读取 Hbase 数据
- java - 使用 django 为 android 应用程序创建管理面板
- symfony - 如何在 Symfony 上自定义由 twig 制作的表单的样式?
- windows - .appxbundle 在我尝试安装时出现文件系统错误
- netsuite - 在设置字段值后如何确保采购不同的值。我可以执行我的其余代码