iOS - 判斷使用者是否允許推播通知(iOS7/iOS8)

來源:互聯網
上載者:User

標籤:

(iOS8中使用者開啟的推播通知類型對應的是UIUserNotificationType(下邊代碼中UIUserNotificationSettings的types屬性的類型),iOS7對應的是UIRemoteNotificationType)

 

此處以iOS8的UIUserNotificationType為例,(如)當本地通知或push/遠程通知 推送時,這個常量指明了app如何去提醒使用者(比如:Badge,Sound,Alert的組合)

那麼如何獲得呢,在iOS8中是通過types屬性,[[UIApplication sharedApplication] currentUserNotificationSettings].types

如,獲得之後,我們要知道的是這個property儲存了所有你指定的推送類型(Badge,Sound,Alert),而在圖一中我們知道了推送類型對應的bitmask:(以四位二進位為例)

   UIUserNotificationTypeNone    = 0,               == 0000                                 0

   UIUserNotificationTypeBadge   = 1 << 0,      == 0001      1左移0位     2^0 = 1

   UIUserNotificationTypeSound   = 1 << 1,      == 0010      1左移1位     2^1 = 2 

   UIUserNotificationTypeAlert   = 1 << 2,       == 0100      1左移2位    2^2 = 4

(以前老師教c語言的時候說過,還可以把左移當做乘2,右移除2)

假如使用者勾選推送時顯示badge和提示sound,那麼types的值就是3(1+2) ==  0001 & 0010 =  0011    ==  2^0 + 2 ^1 = 3  

所以,如果使用者沒有允許推送,types的值必定為0

 1 /** 2  *  check if user allow local notification of system setting 3  * 4  *  @return YES-allowed,otherwise,NO. 5  */ 6 + (BOOL)isAllowedNotification { 7     //iOS8 check if user allow notification 8     if ([UIDevice isSystemVersioniOS8]) {// system is iOS8 9         UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];10         if (UIUserNotificationTypeNone != setting.types) {11             return YES;12         }13     } else {//iOS714         UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];15         if(UIRemoteNotificationTypeNone != type)16             return YES;17     }18     19     return NO;20 }

下面這個方法我是添加在UIDecive的Category中的,用於判斷當前系統版本是大於iOS8還是小於iOS8的

 1 /** 2  *  check if the system version is iOS8 3  * 4  *  @return YES-is iOS8,otherwise,below iOS8 5  */ 6 + (BOOL)isSystemVersioniOS8 { 7     //check systemVerson of device 8     UIDevice *device = [UIDevice currentDevice]; 9     float sysVersion = [device.systemVersion floatValue];10     11     if (sysVersion >= 8.0f) {12         return YES;13     }14     return NO;15 }

 

iOS - 判斷使用者是否允許推播通知(iOS7/iOS8)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.