【问题标题】:How to detect ios push notification permissions/settings programmatically如何以编程方式检测 ios 推送通知权限/设置
【发布时间】:2023-03-14 18:30:01
【问题描述】:

在通知设置(Settings->Notifications->AnyAppName)中有5个项目,每个项目都有一个开关按钮,Sounds,Badge App Icon,Show on Lock Screen,Show in History,Show as Banners

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

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

【问题讨论】:

    标签: ios permissions notifications settings


    【解决方案1】:

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

    • notificationCenterSetting (Show in History)
    • lockScreenSetting (Show on Lock Screen)
    • alertSetting (Show as Banners)
    • 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
                }
            }
        }
    }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 2023-04-05
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多