gpt4 book ai didi

ios - 需要有关在 iOS 中请求通知权限的说明

转载 作者:行者123 更新时间:2023-11-28 05:56:48 25 4
gpt4 key购买 nike

Info about "authorization"

Info about "requesting permission"

问题是它们都需要用在相同的代码中,但它们被分成了 2 篇单独的文章。所以不清楚如何同时处理它们以及它们之间有什么区别(当然输入参数除外)。

我找到的代码只是顺序调用了这些函数:

UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { granted, error in
...
})
UIApplication.shared.registerForRemoteNotifications()

是否正确?这些方法有什么区别?

附言根据文档,我也不能简单地将它们放在 application:didFinishLoad: 中,因为应用程序不应从第一次运行时请求权限。

最佳答案

这个

UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { granted, error in
...
// code here
})

询问用户是否接受实际上会显示弹出窗口的接收通知,但这(用于推送通知而非本地)

UIApplication.shared.registerForRemoteNotifications()

根据 Docs

Call this method to initiate the registration process with Apple Push Notification service. If registration succeeds, the app calls your app delegate object’s application:didRegisterForRemoteNotificationsWithDeviceToken: method and passes it a device token.

//

if #available(iOS 10.0, *) {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })

// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self

} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}

application.registerForRemoteNotifications()

关于ios - 需要有关在 iOS 中请求通知权限的说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51284446/

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