iOS开发之判断用户是否打开APP通知开关

 

标签: enabledRemoteNotificiOS通知iOS是否开启推送开关currentUserNotificatios

 

1.最近在作app内部的推送开关功能。 这样顾客能够本身定义推送给他的内容,屏蔽不想要的一些烦人推送。 服务器

 

在开发过程当中, 若是顾客打开推送开关的时候,也已经向服务器发送指令,进行推送开关同步,给它说这个用户已经打开了A推送,如今服务器推送A推送给客户端, 这时候照说,客服端是能够收到通知的,可是客服端却没有收到。 这是为何呢? 很简单的一个问题,原来是顾客没有在系统通知处打开app的通知开关,因此收不到推送是正常现象。 app

 

那如今就产生了一个需求:spa

       用户在进行设置推送开关的时候,当用户打开推送开关为开的时候,须要去判断 系统通知处的 推送开关用户有没有进行设置?.net

       网上这样的代码都是一大把,关于怎么去检测系统通知有没有打开,可是,发现运用到程序中,没什么鸟用, 会一直提示说,用户没有打开推送。code

下面就到了贴代码的环节了,首先先看下这段代码:对象

 

 

  1. if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes]  == UIRemoteNotificationTypeNone) { //判断用户是否打开通知开关  
  2.         }  

[objc] view plain copyblog

 在CODE上查看代码片派生到个人代码片

  1. typedef NS_OPTIONS(NSUInteger, UIRemoteNotificationType) {//这个是用户当前通知的几种状态,第一种就是用户没有开大通知开关  
  2.     UIRemoteNotificationTypeNone    = 0,  
  3.     UIRemoteNotificationTypeBadge   = 1 << 0,  
  4.     UIRemoteNotificationTypeSound   = 1 << 1,  
  5.     UIRemoteNotificationTypeAlert   = 1 << 2,  
  6.     UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3,  
  7. } NS_ENUM_DEPRECATED_IOS(3_0, 8_0, "Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.") __TVOS_PROHIBITED;  

 

我在程序中就是用到了这个方法,去进行检测,用户有没有打开推送开关, 却忽视了 ip

 

  1. NS_ENUM_DEPRECATED_IOS(3_0, 8_0, "Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.") __TVOS_PROHIBITED  

 


 

经过上面的图,能够看到此方法在iOS8.0就废弃了,虽说并无被移除,可是也要用他最新建议的方法去进行版本的兼容,如今加上在iOS9.0上用这个方法进行判断没有任何做用,如今就用新版的判断方法。下面是新版的判断方法。开发

 

[objc] view plain copy

 在CODE上查看代码片派生到个人代码片

  1. if ([[UIApplication sharedApplication] currentUserNotificationSettings].types  == UIRemoteNotificationTypeNone) {     
  2.        }  

注意: [objc] view plain copy

 在CODE上查看代码片派生到个人代码片

  1. currentUserNotificationSettings 是一个对象,属于UIUserNotificationSettings类  

因此.types切莫忘记。

 

 

总结:

 最完善的作法就是,进行二者兼容 iOS7下的也要兼容, iOS7以上的我更要兼容啦,最完善的作法

  

 

#define IOS8 ([[[UIDevice currentDevice] systemVersion] doubleValue] >=8.0 ? YES : NO)

 

 

  1. if (IOS8) { //iOS8以上包含iOS8  
  2.         if ([[UIApplication sharedApplication] currentUserNotificationSettings].types  == UIRemoteNotificationTypeNone) {  
  3.     }  
  4.  }else{ // ios7 一下      
  5.          if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes]  == UIRemoteNotificationTypeNone) {  
  6.   }  
  7. }  
相关文章
相关标签/搜索