最近極光推送更新到V3版本之后,推送又不成功!配合服務器聯調了半天,發現是服務器環境配置有問題。
想着就把極光推送的步驟給記錄下來。
一、配置push證書
這個可以到極光文檔里寫,很詳細
二、導入必要的框架
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
Xcode7需要的是libz.tbd;Xcode7以下版本是libz.dylib
三、代碼中注冊極光推送
首先在AppDelegate.m
導入#import "JPUSHService.h"
在~didFinishLaunchingWithOptions
方法貼上核心代碼:
// 1.注冊系統通知 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]){ UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]; [application registerUserNotificationSettings:settings]; }
// 2.注冊極光推送 if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound; [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { //可以添加自定義categories [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } [JPUSHService setupWithOption:launchOptions appKey:@"注冊極光生成的appKey" channel:@"Publish channel" apsForProduction:NO advertisingIdentifier:nil]; // 接收應用內消息 NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil]; // 極光推送登錄成功后可以注冊別名 [defaultCenter addObserver:self selector:@selector(registerAlias:) name:kJPFNetworkDidLoginNotification object:nil];
// 3.注冊 DeviceToken - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [JPUSHService registerDeviceToken:deviceToken]; }
四、接收通知
#pragma mark - JPUSHRegisterDelegate // iOS 10 Support,前台收到通知,后台不會執行這個方法 - (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); // 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設置 // 通知內容為:notification.request.content.body } // iOS 10 Support,用戶點擊了通知進入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]; } completionHandler(); // 系統要求執行這個方法 }
#pragma mark 解析極光推送的應用內消息 - (void)networkDidReceiveMessage:(NSNotification *)notification { NSDictionary * userInfo = [notification userInfo]; NSLog(@"解析極光推送的應用內消息:%@",userInfo); } // 接收到遠程通知之后 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // Required,For systems with less than or equal to iOS6 [JPUSHService handleRemoteNotification:userInfo]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // IOS 7 Support Required [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); } //獲取 deviceToken 失敗后 遠程推送(極光推送)打開失敗 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //Optional NSLog(@"獲取 device token 失敗 %@", error); }
五、注冊別名(可選)
主要是在項目的登錄成功或者自動登錄后,使用用戶的唯一標示進行綁定
// 注冊別名 [JPUSHService setAlias:@"一般為用戶的賬號" callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self]; // 極光別名注冊的回調方法 -(void)tagsAliasCallback:(int)iResCode tags:(NSSet*)tags alias:(NSString*)alias { NSLog(@"極光別名注冊的回調方法rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, tags , alias); if (iResCode == 0) { // 注冊成功 } }
去除綁定
用戶進行退出登錄的方法里添加去除綁定即可,值得注意的是用到即時通訊的話,被擠下線也要去除綁定,
//沒有值就代表去除 [JPUSHService setTags:[NSSet set]callbackSelector:nil object:self]; [JPUSHService setAlias:@"" callbackSelector:nil object:self]; [JPUSHService setTags:[NSSet set] alias:@"" callbackSelector:nil target:self];