最近公司產(chǎn)品需求械姻,在項目里面集成極光推送肾筐,配合后臺哆料,實現(xiàn)特定用戶,分組用戶和全部用戶的推送吗铐。之前一直都是接手別人的項目东亦,這些基礎服務都已經(jīng)集成好了,這次算是第一次正式接觸推送唬渗,全當是給自己做筆記了典阵。
- 首先當然是去極光官網(wǎng)創(chuàng)建app獲取到AppKey,然后上傳推送證書到極光,證書不會的同學可以去極光這邊查看配置證書的詳細方法點我查看配置證書
- 配置好完之后就是下載極光的SDK镊逝,使用cocoapods的同學可以通過cocoapods導入壮啊,然后添加依賴庫。Xcode8以上需要開啟Application Target的Capabilities->Push Notifications選項撑蒜,Xcode7以上需要在plist中配置允許http傳輸歹啼,這個一般都有配置就不多說。
- 現(xiàn)在就是在項目里面集成座菠,首先是導入頭文件
// 引入JPush功能所需頭文件
#import "JPUSHService.h"
// iOS10注冊APNs所需頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
然后在app的啟動方法里面添加初始化代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//你的其他代碼
BOOL isPushProduction = YES;
#ifdef DEBUG
isPushProduction = NO;
#endif
//Required 極光推送
//notice: 3.0.0及以后版本注冊可以這樣寫狸眼,也可以繼續(xù)用之前的注冊方式
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// 可以添加自定義categories
// NSSet<UNNotificationCategory *> *categories for iOS10 or later
// NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
[JPUSHService setupWithOption:launchOptions appKey:@"808b05619bc76f8b083c1f8a"
channel:@"appStore"
apsForProduction:isPushProduction
advertisingIdentifier:nil];
注冊成功之后,會在這個方法里面回調(diào)DeviceToken
- (void)application:(UIApplication *)app
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
//一開始我沒看APi浴滴,就網(wǎng)上找的方法把deviceToken轉化成字符串拓萌,把這個當registrationID傳給后臺,后來發(fā)現(xiàn)是錯誤的升略,正確方法看下面
// NSString *deviceTokenString = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""]
// stringByReplacingOccurrencesOfString:@">" withString:@""]
// stringByReplacingOccurrencesOfString:@" " withString:@""];
/// Required - 注冊 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
//取得registrationID保存下來微王,之后和用戶標示一起發(fā)給后臺,極光那邊的API還有一個方法+(NSString *)registrationID可以獲的品嚣,但是建議用下面這個
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
[[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"deviceTokenString"];
}];
}
之后就是收到推送的處理
//iOS7及以上接受到通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
if (application.applicationState == UIApplicationStateActive) {
//iOS7及以上iOS10以下app在前臺運行時接受到推送消息的處理回調(diào)
}else{
//iOS7及以上iOS10以下app在后臺運行時或未啟動時點擊推送消息后的處理回調(diào)
[self handleNotificationActionWithInfo:userInfo];
}
//說明:iOS10以下app在前臺的時候炕倘,接受到消息不會顯示在消息欄,也沒有向下彈出的通知翰撑,iOS10可以自己設置罩旋,下面有說明,如果在iOS10以下需要app在前臺時有向下的通知需要自定義额嘿。
}
// iOS 10 app處于前臺時接受到消息
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個方法,選擇是否提醒用戶劣挫,有Badge册养、Sound、Alert三種類型可以選擇設置
}
// iOS 10 app在后臺或未激活時點擊推送處理事件
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
[self handleNotificationActionWithInfo:userInfo];
completionHandler(); // 系統(tǒng)要求執(zhí)行這個方法
}
- (void)handleNotificationActionWithInfo:(NSDictionary *)info{
//你要進行的處理压固,跳轉頁面還是怎樣球拦,info里面時后臺傳過來的字段
}
- 這樣就可以實現(xiàn)極光推送的基本功能,然后就是配合后臺實現(xiàn)分組推送和特定用戶推送。
//我們這邊是用戶登陸或者切換賬號之后坎炼,將用戶的ID和之前保存的registrationId一起發(fā)給后臺愧膀,后臺就能通過用戶id找到極光那邊對應的用戶進行推送
NSMutableDictionary * param = [NSMutableDictionary dictionary];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"][@"ID"] != nil) {
[param setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"][@"ID"] forKey:@"id"];
}
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"deviceTokenString"] != nil) {
[param setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"deviceTokenString"] forKey:@"registrationId"];
}
//將數(shù)據(jù)傳給后臺,這是我們的請求谣光,可根據(jù)不同情況自行處理
[XYYHttpApi postRequestWithUrl:[XYYUrl stringByAppendingString:@"bindJpush"] Param:param IsNeedSign:YES SuccessBlock:^(NSDictionary *resultData) {
block(YES,nil,resultData);
} FailureBlock:^(NSDictionary *resultData) {
block(NO,resultData[@"msg"],nil);
}];
我們這邊有一個分區(qū)域推送的功能檩淋,我們是給用戶打tags,不同區(qū)域tags不同萄金,后臺可以通過tags去進行分區(qū)域推送
//dealDataArr為后臺傳給我的標示數(shù)組蟀悦,這邊一定要是字符串的數(shù)組,開始后臺給我的不是字符串類型氧敢,導致一直推送失敗日戈,后來找到原因就是這里不是字符串的問題
NSSet * set = [[NSSet alloc] initWithArray:dealDataArr];
[JPUSHService setTags:set completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
} seq:0];
//這個在用戶登陸之后向后臺請求該用戶的tags之后傳給極光,用戶退出登陸之后清空掉
[JPUSHService cleanTags:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
} seq:0];
- 以上差不多就是集成極光推送的過程孙乖,有文檔說明很簡單浙炼,但是細節(jié)方面有若干坑,僅此記錄一下唯袄,提醒自己