你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 解析iOS10中的極光推送新聞的適配

解析iOS10中的極光推送新聞的適配

編輯:IOS開發綜合

IOS10宣布後,發明項目中的極光推送吸收新聞異常了。

查了相干材料後才發明,IOS10中關於告訴做了很多轉變。同時也發明極光也很快更新了對應的SDK。

如今就把適配修正的做法分享一下,願望對有須要的童鞋有所贊助。

詳細做法以下:

留意:必需先裝置Xcode8.0版本。

1、添加相干的SKD,或framework文件

1、添加UserNotification.framework

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

2、停止途徑和新聞推送的設置裝備擺設

1、設置jpush的SDK的途徑

2、開啟新聞推送功效

3、代碼修正

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中的極光推送新聞的適配,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對本站網站的支撐!

【解析iOS10中的極光推送新聞的適配】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved