Swift ios 检查是否在 ios9 和 ios10 中启用了远程推送通知 [英] Swift ios check if remote push notifications are enabled in ios9 and ios10

查看:23
本文介绍了Swift ios 检查是否在 ios9 和 ios10 中启用了远程推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查用户是否在 ios 9 或 ios 10 上启用了远程通知?

How can I check if the user has enabled remote notifications on ios 9 or ios 10?

如果用户没有允许或点击否,我想切换一条消息,询问他们是否要启用通知.

If the user has not allowed or clicked No I want to toggle a message asking if they want to enable notifications.

推荐答案

iOS 10 使用 UNUserNotificationCenter 后更新的答案.

Updated answer after iOS 10 is using UNUserNotificationCenter .

首先你需要import UserNotifications 然后

let current = UNUserNotificationCenter.current()
        current.getNotificationSettings(completionHandler: { permission in
            switch permission.authorizationStatus  {
            case .authorized:
                print("User granted permission for notification")
            case .denied:
                print("User denied notification permission")
            case .notDetermined:
                print("Notification permission haven't been asked yet")
            case .provisional:
                // @available(iOS 12.0, *)
                print("The application is authorized to post non-interruptive user notifications.")
            case .ephemeral:
                // @available(iOS 14.0, *)
                print("The application is temporarily authorized to post notifications. Only available to app clips.")
            @unknown default:
                print("Unknow Status")
            }
        })


这段代码会一直工作到 iOS 9,对于 iOS 10,请使用上面的代码片段.


this code will work till iOS 9, for iOS 10 use the above code snippet.

let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if isRegisteredForRemoteNotifications {
     // User is registered for notification
} else {
     // Show alert user is not registered for notification
}

这篇关于Swift ios 检查是否在 ios9 和 ios10 中启用了远程推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆