iOS 10以上同步獲取通知設(shè)置

最近在做一個(gè)關(guān)于UNUserNotification通知的需求叛复,需要獲取當(dāng)前APP的通知權(quán)限仔引,由于iOS10以上通知使用的API發(fā)生了很大的變化扔仓,在iOS 10以下獲取通知設(shè)置(notification settings)代碼如下:

 UIUserNotificationSettings *theSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
return [theSettings types] & UIUserNotificationTypeAlert;

?而 iOS10采用UNUserNotificationCenter代替了原有的API。使用UNUserNotificationCenter獲取APP通知設(shè)置代碼如下:

   UNUserNotificationCenter *notificationCenter=[UNUserNotificationCenter currentNotificationCenter];
        [notificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            switch (settings.authorizationStatus) {
                case UNAuthorizationStatusAuthorized:
                    enabled = YES;
                    break;
                default:
                    break;
            }
        }];

getNotificationSettingsWithCompletionHandler:采用的是異步回調(diào)的過程咖耘。為了寫一個(gè)兼容兩種類型的函數(shù)翘簇,我的代碼如下:

   __block BOOL enabled = NO;
    if (@available(iOS 10.0, *)) {
        UNUserNotificationCenter *notificationCenter=[UNUserNotificationCenter currentNotificationCenter];
        [notificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            switch (settings.authorizationStatus) {
                case UNAuthorizationStatusAuthorized:
                    enabled = YES;
                    break;
                default:
                    break;
            }
        }];
    } else {
       UIApplication *application = [UIApplication sharedApplication];
        if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
            UIUserNotificationSettings *settings = [application currentUserNotificationSettings];
            if (settings.types != UIUserNotificationTypeNone) {
                enabled = YES;
            }
        }
    }

這樣貌似很完美,其實(shí)這段代碼犯了一個(gè)很低級(jí)的錯(cuò)誤鲤看。把異步當(dāng)成同步再用缘揪。當(dāng)時(shí)發(fā)現(xiàn)這個(gè)問題時(shí),APP已經(jīng)上線了义桂,最后只好向領(lǐng)導(dǎo)老實(shí)認(rèn)錯(cuò)寫檢討找筝。傷心太平洋(πーπ)

出現(xiàn)錯(cuò)誤肯定要改嘛,最后想到了用信號(hào)量來強(qiáng)制將異步變?yōu)橥娇兜酰暮蟮拇a如下:

  __block BOOL enabled = NO;
    if (@available(iOS 10.0, *)) {
        dispatch_semaphore_t sem;
        sem = dispatch_semaphore_create(0);
        UNUserNotificationCenter *notificationCenter=[UNUserNotificationCenter currentNotificationCenter];
        [notificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            switch (settings.authorizationStatus) {
                case UNAuthorizationStatusAuthorized:
                    enabled = YES;
                    break;
                default:
                    break;
            }
            dispatch_semaphore_signal(sem);
        }];
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); //獲取通知設(shè)置的過程是異步的袖裕,這里需要等待
    } else {
          UIApplication *application = [UIApplication sharedApplication];
        if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
            UIUserNotificationSettings *settings = [application currentUserNotificationSettings];
            if (settings.types != UIUserNotificationTypeNone) {
                enabled = YES;
            }
        }
    } 

另附上Stack Overflow一個(gè)獲取Notification Settings通用的類,
地址
.h文件

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger , PushNotificationType) {
    PushNotificationTypeNone    = 0,      // the application may not present any UI upon a notification being received
    PushNotificationTypeBadge   = 1 << 0, // the application may badge its icon upon a notification being received
    PushNotificationTypeSound   = 1 << 1, // the application may play a sound upon a notification being received
    PushNotificationTypeAlert   = 1 << 2, // the application may display an alert upon a notification being received
};

@interface SpecificPush : NSObject

@property (nonatomic, readonly) PushNotificationType currentNotificationSettings;

+ (SpecificPush *)sharedInstance;
- (PushNotificationType)types;

@end

.m文件

#import <UserNotifications/UserNotifications.h>

@interface SpecificPush()
@property (nonatomic) PushNotificationType currentNotificationSettings;
@end

@implementation SpecificPush

#pragma mark - Init

static SpecificPush *instance = nil;
+ (SpecificPush *)sharedInstance
{
    @synchronized (self)
    {
        if (instance == nil)
        {
            [SpecificPush new];
        }
    }
    return instance;
}

- (instancetype)init
{
    NSAssert(!instance, @"WARNING - Instance of SpecifishPush already exists");
    self = [super init];
    if (self)
    {
        self.currentNotificationSettings = PushNotificationTypeNone;
    }
    instance = self;
    return self;
}

- (PushNotificationType)types
{
    if (@available(iOS 10.0, *))
    {
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

        [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
            if ((settings.soundSetting == UNNotificationSettingDisabled) && (settings.alertSetting == UNNotificationSettingDisabled) && (settings.soundSetting == UNNotificationSettingDisabled))
            {
                self.currentNotificationSettings = PushNotificationTypeNone;
            }
            if (settings.badgeSetting == UNNotificationSettingEnabled)
            {
                self.currentNotificationSettings = PushNotificationTypeBadge;
            }
            if (settings.soundSetting == UNNotificationSettingEnabled)
            {
                self.currentNotificationSettings = PushNotificationTypeSound;
            }

            if (settings.alertStyle == UNNotificationSettingEnabled)
            {
                self.currentNotificationSettings = PushNotificationTypeAlert;
            }

            dispatch_semaphore_signal(semaphore);

        }];

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_release(semaphore);
    }
    else
    {
        UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];

        if (settings.types == UIUserNotificationTypeNone)
        {
            self.currentNotificationSettings = PushNotificationTypeNone;
        }
        if (settings.types & UIUserNotificationTypeBadge)
        {
            self.currentNotificationSettings = PushNotificationTypeBadge;
        }
        if (settings.types & UIUserNotificationTypeSound)
        {
            self.currentNotificationSettings = PushNotificationTypeSound;
        }
        if (settings.types & UIUserNotificationTypeAlert)
        {
            self.currentNotificationSettings = PushNotificationTypeAlert;
        }
    }

    return self.currentNotificationSettings;
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末溉瓶,一起剝皮案震驚了整個(gè)濱河市急鳄,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌堰酿,老刑警劉巖疾宏,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異触创,居然都是意外死亡坎藐,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門哼绑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來岩馍,“玉大人,你說我怎么就攤上這事抖韩≈鳎” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵茂浮,是天一觀的道長双谆。 經(jīng)常有香客問我,道長席揽,這世上最難降的妖魔是什么佃乘? 我笑而不...
    開封第一講書人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮驹尼,結(jié)果婚禮上趣避,老公的妹妹穿的比我還像新娘。我一直安慰自己新翎,他們只是感情好程帕,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開白布住练。 她就那樣靜靜地躺著,像睡著了一般愁拭。 火紅的嫁衣襯著肌膚如雪讲逛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評(píng)論 1 297
  • 那天岭埠,我揣著相機(jī)與錄音盏混,去河邊找鬼。 笑死惜论,一個(gè)胖子當(dāng)著我的面吹牛许赃,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播馆类,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼混聊,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了乾巧?” 一聲冷哼從身側(cè)響起句喜,我...
    開封第一講書人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎沟于,沒想到半個(gè)月后咳胃,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡旷太,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年展懈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片泳秀。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡标沪,死狀恐怖榄攀,靈堂內(nèi)的尸體忽然破棺而出嗜傅,到底是詐尸還是另有隱情,我是刑警寧澤檩赢,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布吕嘀,位于F島的核電站,受9級(jí)特大地震影響贞瞒,放射性物質(zhì)發(fā)生泄漏偶房。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一军浆、第九天 我趴在偏房一處隱蔽的房頂上張望棕洋。 院中可真熱鬧,春花似錦乒融、人聲如沸掰盘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽愧捕。三九已至奢驯,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間次绘,已是汗流浹背瘪阁。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留邮偎,地道東北人管跺。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像钢猛,于是被迫代替她去往敵國和親伙菜。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353