在应用程序启动后请求通知权限时,不会调用 didRegisterForRemoteNotificationsWithDeviceToken
didRegisterForRemoteNotificationsWithDeviceToken isn't called when requesting notification permission after app launch
我正在关注这里的推送通知教程:
https://www.raywenderlich.com/11395893-push-notifications-tutorial-getting-started
我已经在测试设备上完成了所有工作,在允许通知后,我可以在 didRegisterForRemoteNotificationsWithDeviceToken 运行s 时获取设备令牌。
现在我想在请求通知权限时更改。我不想像教程中那样请求用户允许应用程序启动通知。我只想稍后再问,一旦用户登录。但是,当我调用 AppDelegate 注册通知时,它会提示用户,但是当他们接受时,它不会 运行 didRegisterForRemoteNotificationsWithDeviceToken 函数,它具有我需要发送到后端的设备令牌。给出了什么?
AppDelegate.swift
import SwiftUI
import UserNotifications
class AppDelegate: NSObject, UIApplicationDelegate {
@EnvironmentObject var authenticatedUser: AuthenticatedUser
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
return true
}
func registerForPushNotifications() {
UNUserNotificationCenter.current()
.requestAuthorization(
options: [.alert, .sound, .badge]) { [weak self] granted, _ in
print("Permission granted: \(granted)")
guard granted else { return }
self?.getNotificationSettings()
}
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
// this function never gets called!
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
print("Device Token: \(token)")
// send token to backend here
}
func application(
_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error
) {
print("Failed to register: \(error)")
}
}
MyGroupView.swift我要提示通知权限的地方
import SwiftUI
struct MyGroupView: View {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@EnvironmentObject var authenticatedUser: AuthenticatedUser
// other variables and state here...
var body: some View {
GeometryReader { proxy in
VStack {
// content here...
}
.onAppear() {
appDelegate.registerForPushNotifications()
}
.edgesIgnoringSafeArea(.bottom)
}
}
// other functions...
}
在您的 AppDelegates didFinishLaunchingWithOptions
中,您需要设置委托
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
我正在关注这里的推送通知教程: https://www.raywenderlich.com/11395893-push-notifications-tutorial-getting-started
我已经在测试设备上完成了所有工作,在允许通知后,我可以在 didRegisterForRemoteNotificationsWithDeviceToken 运行s 时获取设备令牌。
现在我想在请求通知权限时更改。我不想像教程中那样请求用户允许应用程序启动通知。我只想稍后再问,一旦用户登录。但是,当我调用 AppDelegate 注册通知时,它会提示用户,但是当他们接受时,它不会 运行 didRegisterForRemoteNotificationsWithDeviceToken 函数,它具有我需要发送到后端的设备令牌。给出了什么?
AppDelegate.swift
import SwiftUI
import UserNotifications
class AppDelegate: NSObject, UIApplicationDelegate {
@EnvironmentObject var authenticatedUser: AuthenticatedUser
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
return true
}
func registerForPushNotifications() {
UNUserNotificationCenter.current()
.requestAuthorization(
options: [.alert, .sound, .badge]) { [weak self] granted, _ in
print("Permission granted: \(granted)")
guard granted else { return }
self?.getNotificationSettings()
}
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
// this function never gets called!
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
print("Device Token: \(token)")
// send token to backend here
}
func application(
_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error
) {
print("Failed to register: \(error)")
}
}
MyGroupView.swift我要提示通知权限的地方
import SwiftUI
struct MyGroupView: View {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@EnvironmentObject var authenticatedUser: AuthenticatedUser
// other variables and state here...
var body: some View {
GeometryReader { proxy in
VStack {
// content here...
}
.onAppear() {
appDelegate.registerForPushNotifications()
}
.edgesIgnoringSafeArea(.bottom)
}
}
// other functions...
}
在您的 AppDelegates didFinishLaunchingWithOptions
中,您需要设置委托
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}