gpt4 book ai didi

ios - UNUserNotificationCenter didReceive 未被调用

转载 作者:搜寻专家 更新时间:2023-10-31 22:58:36 25 4
gpt4 key购买 nike

当我发送推送通知时,应用程序在前台 willPresent 被调用。 didReceive 永远不会被调用。当应用程序在后台并收到推送通知时,会显示警报,但应用程序不会调用 didReceivewillPresent

在 Project > Capabilities 中,我检查了后台模式 Location updatesRemote notifications。位置更新用于不相关的代码。

我还启用了推送通知。这工作正常,因为他们正在收到。

我已经在我的 AppDelegate 中实现了 UNUserNotificationCenter 通知内容,如下所示:

import UserNotifications
...

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
...

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
...
registerForPushNotifications(application: application)
...
}

// MARK: - Push Notifications

func registerForPushNotifications(application: UIApplication) {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
notificationCenter.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
if (granted)
{
UIApplication.shared.registerForRemoteNotifications()
}
else{
//Do stuff if unsuccessful...
print("Unsuccessful in registering for push notifications")
}
})
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
//convert the deviceToken to a string
let deviceTokenString = deviceToken.map { String(format: "%02.2hhx", arguments: [$0]) }.joined()

let ud = UserDefaults.standard
ud.set(deviceTokenString, forKey: "deviceToken")
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//Handle the notification
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//handle the notification
print("Push notification: ",response.notification.request.content.userInfo)

... code to import the notification data into Core Data.

completionHandler()
}

...
}

知道为什么 didReceive 没有被调用吗?

这是收到的推送通知:

[AnyHashable("aps"): {
alert = "[name] has added you as a friend.";
category = "NEWS_CATEGORY";
"related_user_id" = 55;
sound = default;
type = "friend_request";
}]

iOS 10.1、Swift 3。

最佳答案

我发现在获得权限后我需要设置delegate。

否则,当您第一次使用应用程序午餐时,您在获得权限之前注册了委托(delegate)并且没有触发 didReceive 函数 - 这仅在您第一次运行应用程序时发生,在终止并重新启动后它被调用就好了。

见下文:

 UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { (granted, error) in
if granted {
// Register after we get the permissions.
UNUserNotificationCenter.current().delegate = self
}
})

关于ios - UNUserNotificationCenter didReceive 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40758200/

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