gpt4 book ai didi

ios - 如何以编程方式检测 ios 推送通知权限/设置

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:19 27 4
gpt4 key购买 nike

在通知设置中(Settings->Notifications->AnyAppName),有5个项目,每个项目都有一个切换按钮,SoundsBadge App Icon在锁定屏幕上显示在历史记录中显示显示为横幅

我正在使用 [[[UIApplication sharedApplication] currentUserNotificationSettings] types] 来获取用户的设置并提升相应的警报以供使用。它可能返回值 0~7 代表SoundBadgeBanners 的任意组合。问题是,我们是否能够检测到 Show on Lock ScreenShow in History 的状态?

另外,在设置页面的底部,有一个名为Show PreviewsOPTIONS选项,它有三个选项:Always(Default)When UnlockedNever。我们是否能够为此以编程方式获取用户设置?

最佳答案

您应该使用从 iOS 10 开始支持的 UserNotifications 框架。这将允许您通过 UNUserNotificationCentergetNotificationSettingsWithCompletionHandler: 函数检索 UNNotificationSettings。在 UNNotificationSettings 中,您可以检查一些值:

  • notificationCenterSetting(在历史记录中显示)
  • lockScreenSetting(在锁定屏幕上显示)
  • alertSetting(显示为横幅)
  • alertStyle(横幅的类型)

例如:

[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if(settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
//notifications are enabled for this app

if(settings.notificationCenterSetting == UNNotificationSettingEnabled ||
settings.lockScreenSetting == UNNotificationSettingEnabled ||
(settings.alertSetting == UNNotificationSettingEnabled && settings.alertStyle != UNAlertStyleNone)) {
//the user will be able to see the notifications (on the lock screen, in history and/or via banners)

dispatch_async(dispatch_get_main_queue(), ^(){

//now for instance, register for remote notifications
//execute this from the main queue
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
else {
//the user must change notification settings in order te receive notifications
}
}
else {
//request authorization
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted) {
dispatch_async(dispatch_get_main_queue(), ^(){

//now for instance, register for remote notifications
//execute this from the main queue
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
else {
//user denied the authorization request
//the user must change notification settings in order te receive notifications
}
}
}
}];

关于ios - 如何以编程方式检测 ios 推送通知权限/设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48630537/

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