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

Hem*_*ang 8 objective-c apple-push-notifications ios ios9 ios10

此问题特定于iOS 10 APNS更改.

这是我的应用程序的流程:

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

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

有没有任何简洁明了的解决方案,以便我们可以支持iOS 10更改,同时仍然支持iOS 8或9?

Din*_*yas 7

使用此代码 -

if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
// yes
}else{
// no
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*ley 7

UIUserNotificationSettings在iOS8中已弃用.如果要访问应用程序设置的常规状态,请查看新框架UNUserNotifications.我的理解是它将推动和本地视为一件事.注册通知后,您可以调用注册推送.但是对于本地权限 - 标记等,您仍需要请求用户权限.也就是说,您的设备可以在没有用户许可的情况下接受推送通知以接收数据更新,但您只能通过具有权限的中心显示通知.以下是如何查看已授予的权限.

  1. 将框架导入您的班级

    @import UserNotifications;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 查询设置

      - (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");
    
      }];
    }
    
    Run Code Online (Sandbox Code Playgroud)


Dar*_*ana 3

每当您的应用程序进入前台时,您都可以使用getNotificationSettingsWithCompletionHandler 。

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

/////////////////////

以下是原始答案,但 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");
        }
Run Code Online (Sandbox Code Playgroud)

如果您想检查状态是否较前一个发生变化,您可以保留currentUserNotificationSettings某个变量的前一个值,并在方法中超时将其与当前值进行比较applicationWillEnterForeground