首页 文章

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

提问于
浏览
0

Info about "authorization"

Info about "requesting permission"

问题是它们都需要在相同的代码中,但它们被分成两个单独的文章 . 所以目前还不清楚如何同时处理它们以及它们之间的区别(当然除了输入参数之外) .

我发现的代码只是按顺序调用这些函数:

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

这是对的吗?这些方法有什么区别?

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

1 回答

  • 0

    这个

    UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { granted, error in
    

    ......})

    询问用户是否接受实际将显示弹出窗口的接收通知,但是此(用于推送通知不是本地的)

    UIApplication.shared.registerForRemoteNotifications()
    

    根据Docs

    调用此方法以使用Apple推送通知服务启动注册过程 . 如果注册成功,应用程序将调用您的app delegate对象的应用程序:didRegisterForRemoteNotificationsWithDeviceToken:方法并将其传递给设备令牌 .

    //

    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()
    

相关问题