極光推送官網:https://www.jpush.cn
極光推送IOS官方文檔:http://docs.jpush.cn/display/dev/iOS
集成方式
1、在JPush Portal上創建應用
在JPush的管理Portal上 上傳證書並創建應用。如果對APNs證書不太瞭解 請參考 iOS 證書設置指南,
創建成功後自動生成 AppKey 用以標識該應用。
2、導入API開發包到應用程序項目
將SDK包解壓,在XCode中選擇“Add files to 'Your project name'...”,將解壓後的lib子文件夾(包含APService.h、libPushSDK.a)添加到你的工程目錄中。
3、必要的框架
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
libz.dylib
4、Build Settings
設置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,比如SDK文件夾(默認爲lib)與工程文件在同一級目錄下,則都設置爲"$(SRCROOT)/[文件夾名稱]"即可。
5、創建並配置PushConfig.plist文件
在你的工程中創建一個新的Property List文件,並將其命名爲PushConfig.plist,填入Portal爲你的應用提供的APP_KEY等參數。
{
"APS_FOR_PRODUCTION = "0";
"CHANNEL" = "Publish channel";
"APP_KEY" = "AppKey copied from JPush Portal application";
}
- CHANNEL
- APP_KEY
- APS_FOR_PRODUCTION
在1.2.2或之前版本的配置文件中,有 TEST_MODE 這個鍵,新版的SDK不再使用,可以將它刪除。
6、添加代碼:
在 APPDelegate.m文件中的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加代碼:
//極光推送
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidSetup:) name:kAPNetworkDidSetupNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidClose:) name:kAPNetworkDidCloseNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidRegister:) name:kAPNetworkDidRegisterNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidLogin:) name:kAPNetworkDidLoginNotification object:nil];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kAPNetworkDidReceiveMessageNotification object:nil];
[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
// Required
[APService setupWithOption:launchOptions];
同時在APPDelegate.m 文件中添加代碼
#pragma mark - 推送
- (void)networkDidSetup:(NSNotification *)notification {
NSLog(@"已連接");
}
- (void)networkDidClose:(NSNotification *)notification {
NSLog(@"未連接。。。");
}
- (void)networkDidRegister:(NSNotification *)notification {
NSLog(@"已註冊");
}
- (void)networkDidLogin:(NSNotification *)notification {
NSLog(@"已登錄");
NSLog(@"registrationID:%@",[APService registrationID]);
}
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notification userInfo];
NSString *title = [userInfo valueForKey:@"title"];
NSString *content = [userInfo valueForKey:@"content"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSLog(@"%@",[NSString stringWithFormat:@"收到消息\ndate:%@\ntitle:%@\ncontent:%@", [dateFormatter stringFromDate:[NSDate date]],title,content]);
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[APService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[APService handleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// 取得 APNs 標準信息內容
NSDictionary *aps = [userInfo valueForKey:@"aps"];
NSString *content = [aps valueForKey:@"alert"]; //推送顯示的內容
NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge數量
NSString *sound = [aps valueForKey:@"sound"]; //播放的聲音
NSDictionary *dic = @{@"Lo": [userInfo valueForKey:@"Lo"], @"La": [userInfo valueForKey:@"La"], @"address": [userInfo valueForKey:@"address"]};
[[SRViewController shareInstance] addPointAnnotationForAPNS:dic];
// 取得自定義字段內容
NSLog(@"content =[%@], badge=[%d], sound=[%@]",content,badge,sound);
// Required
// IOS 7 Support Required
[APService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
//設置badge
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[application setApplicationIconBadgeNumber:0];
}