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

首頁 > 系統 > iOS > 正文

解析iOS10中的極光推送消息的適配

2020-07-26 03:11:05
字體:
來源:轉載
供稿:網友

iOS10發布后,發現項目中的極光推送接收消息異常了。

查了相關資料后才發現,iOS10中對于通知做了不少改變。同時也發現極光也很快更新了對應的SDK。

現在就把適配修改的做法分享一下,希望對有需要的童鞋有所幫助。

具體做法如下:

注意:必須先安裝Xcode8.0版本。

一、添加相關的SKD,或framework文件

1、添加UserNotification.framework

2、更新jpush的SDK(最新版本:jpush-ios-2.1.9.a)https://www.jiguang.cn

二、進行路徑和消息推送的配置

1、設置jpush的SDK的路徑

2、開啟消息推送功能

三、代碼修改

1、添加userNotification的頭文件

2、添加userNotification的啟用代碼

3、添加jpush的適配代碼

4、添加jpush的代理和代理方法(注意:在appDelegate.m文件中使用)


補充:完整的使用極光

1、導入相應頭文件

#import "JPUSHService.h" #import <AdSupport/AdSupport.h> #ifdef NSFoundationVersionNumber_iOS_9_x_Max // 這里是iOS10需要用到的框架 #import <UserNotifications/UserNotifications.h> #endif

2、啟動極光推送功能

static NSString *JPushAppKey = @"6abc87b33b23d35b9c3b86e0"; static NSString *JPushChannel = @"Publish channel"; // static BOOL JPushIsProduction = NO; #ifdef DEBUG // 開發 極光FALSE為開發環境 static BOOL const JPushIsProduction = FALSE; #else // 生產 極光TRUE為生產環境 static BOOL const JPushIsProduction = TRUE; #endif [objc] view plain copy 在CODE上查看代碼片派生到我的代碼片- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // 啟動極光推送 // Required // - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { } if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) // iOS10 { #ifdef NSFoundationVersionNumber_iOS_9_x_Max JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init]; entity.types = (UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound); [JPUSHService registerForRemoteNotificationConfig:entity delegate:target]; #endif } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // categories [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } else { // categories nil [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; } // Required // [JPUSHService setupWithOption:launchOptions] // pushConfig.plist appKey // 有廣告符標識IDFA(盡量不用,避免上架審核被拒) /* NSString *JPushAdvertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; [JPUSHService setupWithOption:JPushOptions appKey:JPushAppKey channel:JPushChannel apsForProduction:JPushIsProduction advertisingIdentifier:JPushAdvertisingId]; */ // 或無廣告符標識IDFA(盡量不用,避免上架審核被拒) [JPUSHService setupWithOption:options appKey:JPushAppKey channel:JPushChannel apsForProduction:JPushIsProduction]; // 2.1.9版本新增獲取registration id block接口。 [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) { if(resCode == 0) { // iOS10獲取registrationID放到這里了, 可以存到緩存里, 用來標識用戶單獨發送推送 NSLog(@"registrationID獲取成功:%@",registrationID); [[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"]; [[NSUserDefaults standardUserDefaults] synchronize]; } else { NSLog(@"registrationID獲取失敗,code:%d",resCode); } }]; return YES; }

3、注冊

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [JPUSHService registerDeviceToken:data]; }

4、注冊失敗

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificwationsWithError:(NSError *)error { NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); }

5、接收

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // apn 內容獲取: // 取得 APNs 標準信息內容 [JPUSHService handleRemoteNotification:dict]; }

6、處理通知

6-1、iOS10以下版本時

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { DLog(@"2-1 didReceiveRemoteNotification remoteNotification = %@", userInfo); // apn 內容獲取: [JPUSHService handleRemoteNotification:dict]; completionHandler(UIBackgroundFetchResultNewData); DLog(@"2-2 didReceiveRemoteNotification remoteNotification = %@", userInfo); if ([userInfo isKindOfClass:[NSDictionary class]]) { NSDictionary *dict = userInfo[@"aps"]; NSString *content = dict[@"alert"]; DLog(@"content = %@", content); } if (application.applicationState == UIApplicationStateActive) { // 程序當前正處于前臺 } else if (application.applicationState == UIApplicationStateInactive) { // 程序處于后臺 } }

6-2、iOS10及以上版本時

#pragma mark - iOS10: 收到推送消息調用(iOS10是通過Delegate實現的回調) #pragma mark- JPUSHRegisterDelegate #ifdef NSFoundationVersionNumber_iOS_9_x_Max // 當程序在前臺時, 收到推送彈出的通知 - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler { NSDictionary *userInfo = notification.request.content.userInfo; if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; } // 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以設置 // completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert); } // 程序關閉后, 通過點擊推送彈出的通知 - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { NSDictionary *userInfo = response.notification.request.content.userInfo; if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; } completionHandler(); // 系統要求執行這個方法 } #endif

7、其他注意事項

為了保證用戶能正常接收,或有針對性的接收通知,登錄成功后(或退出后)需要設置別名、標記。通常都是該邏輯都是寫在用戶登錄APP成功之后,或者是用戶退出當前登錄狀態后。

/// 綁定別名(注意:1 登錄成功或者自動登錄后;2 去除綁定-退出登錄后) + (void)JPushTagsAndAliasInbackgroundTags:(NSSet *)set alias:(NSString *)name { // 標簽分組(表示沒有值) NSSet *tags = set; // 用戶別名(自定義值,nil是表示沒有值) NSString *alias = name; NSLog(@"tags = %@, alias = %@(registrationID = %@)", tags, alias, [self registrationID]); // tags、alias均無值時表示去除綁定 [JPUSHService setTags:tags aliasInbackground:alias]; }

以上所述是小編給大家介紹的解析iOS10中的極光推送消息的適配,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洛南县| 天等县| 新兴县| 怀仁县| 大安市| 宁陕县| 大渡口区| 临猗县| 枣庄市| 阜新| 礼泉县| 原阳县| 清丰县| 都安| 通山县| 竹溪县| 军事| 西乌珠穆沁旗| 屯昌县| 晋宁县| 邢台市| 清流县| 西城区| 开封市| 中江县| 余干县| 璧山县| 垣曲县| 两当县| 乌审旗| 亚东县| 象州县| 铁力市| 平南县| 夹江县| 五河县| 喀什市| 荥阳市| 禄劝| 文水县| 清远市|