在IOS8及IOS9中使用UILocationNotification本地推送時不顯示推送

在嘗試使用UILocalNotification中,不論怎麼設置,推送都不會顯示。之前還以爲是本機時間的問題,在本機時間裏設置了半天,還重新看了NSDate類。還是沒弄成功。
最後查閱多方發現:
**

在IOS8以後,要推送通知,都要獲取用戶的權限(即需要用戶同意)纔可以推送

**
想要獲取用戶權限,要添加以下代碼在AppDelegate.m文件中。


AppDelegate.m文件中


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [application registerUserNotificationSettings:setting];
    return YES;
}

UIUserNotificationType 是一個枚舉

在頭文件的原型如下

typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) {
    UIUserNotificationTypeNone    = 0,    
    UIUserNotificationTypeBadge   = 1 << 0, 
    UIUserNotificationTypeSound   = 1 << 1, 
    UIUserNotificationTypeAlert   = 1 << 2,
}

UIUserNotificationTypeNone 表示不推送
UIUserNotificationTypeBadge 表示接受圖標又上角的數值改變(就是強迫症很討厭的99+的那個數字)
UIUserNotificationTypeSound 表示接受聲音
UIUserNotificationTypeAlert 表示接受提醒(橫幅/彈窗)

你想要獲取用戶哪幾種權限,全部寫上即可,中間用” | ” 符號隔開。

然後執行

[application registerUserNotificationSettings:setting];

將你的設置加入用戶註冊通知中。

運行後,就會通知用戶,要獲取權限。好就是獲取權限,不好就是不給權限。

點過一次,下次就不會再提醒了,就算你程序退出了,下次也不會提醒


新手初學IOS,如有什麼錯誤的地方,還請大神指教。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章