国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS10推送通知適配

2019-11-09 13:55:21
字體:
來源:轉載
供稿:網友

iOS10推送新增了UserNotifications Framework,使用起來其實很簡單。

只是在iOS10以上系統上點擊通知欄,回調方法不再走原來的這兩個方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {}

而是在前臺的時候回調

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPResentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

從后臺進入的時候回調

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler

直接說怎么用吧:

1,導入頭文件

#import <UserNotifications/UserNotifications.h>

2,注冊通知

#define IOS8 ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 9.0)#define IOS8_10 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 10.0)#define IOS10 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中

復制代碼
if (IOS10) {        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];        center.delegate = self;        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {            if (!error) {                NSLog(@"succeeded!");            }        }];    } else if (IOS8_10){//iOS8-iOS10        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];        [application registerUserNotificationSettings:settings];        [application registerForRemoteNotifications];    } else {//iOS8以下        [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];    }復制代碼

 

3,回調方法中,獲取通知數據(前臺類似不做說明)

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {    NSDictionary *userInfo = response.notification.request.content.userInfo;   //消息處理}

4,對于本地通知沒有什么變化依然會回調

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柏乡县| 乐业县| 平潭县| 辽中县| 资溪县| 乌鲁木齐市| 双柏县| 西林县| 鹤山市| 通道| 波密县| 奈曼旗| 潮安县| 紫云| 阿拉善左旗| 会泽县| 贵南县| 白沙| 台湾省| 大同县| 武城县| 儋州市| 宽城| 金乡县| 三亚市| 体育| 凤城市| 渭南市| 文化| 务川| 景宁| 凤山县| 淳化县| 江川县| 八宿县| 修武县| 云林县| 九台市| 康马县| 德保县| 米泉市|