如何检查用户是否从设置中启用了推送通知?

11

这个问题与iOS 10的APNS更改相关。

这是应用程序的流程:

  1. 安装应用
  2. 应用启动 ➝ 登录界面
  3. 成功登录 ➝ 主屏幕
  4. 推送通知 ➝ 请求
  5. 推送通知 ➝ 不允许
  6. 关闭应用
  7. 设置 ➝ 用户启用推送通知
  8. 打开应用
  9. 如何检查设置是否更新?
  10. 关闭应用
  11. 设置 ➝ 用户禁用推送通知
  12. 打开应用
  13. 如何检查设置是否更新?

只有当用户登录时,我才会请求推送通知(步骤4)。因此,在用户注销之前,我将无法重新请求推送通知。

是否有一种简洁明了的解决方案,可以支持iOS 10的更改,并同时支持iOS 8或9?


你找到了解决方案吗? - Dory
3个回答

10

在iOS8中,UIUserNotificationSettings已被弃用。如果您想访问应用程序设置的一般状态,请查看UNUserNotifications,这是一个新框架。我的理解是,它将推送和本地通知视为同一事物。当您注册通知时,可以调用注册推送。但是对于本地权限 - 如标记等,仍需要请求用户权限。也就是说,您的设备可以接受推送通知而无需用户许可以接收数据更新,但是您只能通过权限来显示通知。以下是查看已授予的权限的方法。

  1. Import the framework into your class

    @import UserNotifications;
    
  2. Query the settings

      - (void)_queryNotificationsStatus
    {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings){
    
        //1. Query the authorization status of the UNNotificationSettings object
        switch (settings.authorizationStatus) {
        case UNAuthorizationStatusAuthorized:
            NSLog(@"Status Authorized");
            break;
        case UNAuthorizationStatusDenied:
            NSLog(@"Status Denied");
            break;
        case UNAuthorizationStatusNotDetermined:
            NSLog(@"Undetermined");
            break;
        default:
            break;
        }
    
    
        //2. To learn the status of specific settings, query them directly
        NSLog(@"Checking Badge settings");
        if (settings.badgeSetting == UNAuthorizationStatusAuthorized)
        NSLog(@"Yeah. We can badge this puppy!");
        else
        NSLog(@"Not authorized");
    
      }];
    }
    

8
使用这段代码 -
if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
// yes
}else{
// no
}

如果用户已禁用通知,也会返回true。 - pw2

3

当你的应用程序进入前台时,可以使用 getNotificationSettingsWithCompletionHandler

-(void) IsNotifictaionEnabled :(void (^)(BOOL isActive))handler {
    [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        if (settings.alertSetting == UNNotificationSettingEnabled) {
            handler(YES);
        } else {
            handler(NO);
        }
    }];
}

以下是原始答案,但现在currentUserNotificationSettings已经被弃用。

当您的应用程序进入前台时,您可以使用currentUserNotificationSettings

 UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
     if (grantedSettings.types == UIUserNotificationTypeNone) {
            NSLog(@"No permiossion granted");
        }
        else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
            NSLog(@"Sound and alert permissions ");
        }
        else if (grantedSettings.types  & UIUserNotificationTypeAlert){
            NSLog(@"Alert Permission Granted");
        }

如果您想检查状态是否与先前的状态发生了变化,可以将currentUserNotificationSettings的先前值保存到某个变量中,并在applicationWillEnterForeground方法中随时间比较其当前值。


这似乎是用于本地通知的代码,它能用于远程通知吗? - Hemang
不,CurrentUserNotificationSettings适用于两者。- 也适用于远程通知。 - PlusInfosys

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接