最近極光推送更新到V3版本以後,推送又不成功!配合服務器聯調了半天,發現是服務器環境配置有問題。ios
想着就把極光推送的步驟給記錄下來。服務器
1、配置push證書app
這個能夠到極光文檔裏寫,很詳細框架
2、導入必要的框架less
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
Xcode7須要的是libz.tbd;Xcode7如下版本是libz.dylib
3、代碼中註冊極光推送ide
首先在AppDelegate.m
導入#import "JPUSHService.h"
在~didFinishLaunchingWithOptions
方法貼上核心代碼:fetch
// 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]; }
4、接收通知ui
#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); }
5、註冊別名(可選)spa
主要是在項目的登陸成功或者自動登陸後,使用用戶的惟一標示進行綁定code
// 註冊別名 [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];