2016-12-24 119 views
5

此问题特定于iOS 10 APNS更改。如何检查用户是否已启用设置的推送通知?

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

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

我只在用户登录时请求推送通知(第4步)。因此,直到用户注销,我将无法重新请求推送。

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

+0

你有没有找到任何解决办法这一 – Dory

回答

0

只要您的应用程序进入前台,就可以使用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方法进行比较。

+0

这似乎是本地通知代码,它可以用于远程通知吗? – Hemang

+0

不,CurrentUserNotificationSettings适用于两者。 - 将用于远程通知 – Darshana

6

使用此代码 -

if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) { 
// yes 
}else{ 
// no 
} 
3

UIUserNotificationSettings被弃用早在iOS8上。如果您想访问应用程序设置的一般状态,请查看UNUserNotifications这一新框架。我的理解是,它把推动和本地视为一件事。当您注册通知时,您可以致电注册推送。但是对于本地权限 - 徽章等,您仍然需要请求用户权限。也就是说,您的设备可以在没有用户许可的情况下接受推送通知,以便接收数据更新,但您只能通过有权限的中心显示通知。以下是如何查看授予的权限。

  1. 导入框架到类

    @import UserNotifications; 
    
  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"); 
    
        }]; 
    } 
    
相关问题