極光推送(JPush)是一個(gè)端到端的推送服務(wù),使得服務(wù)器端消息能夠及時(shí)地推送到終端用戶手機(jī)上,讓開(kāi)發(fā)者積極地保持與用戶的連接趁餐,從而提高用戶活躍度、提高應(yīng)用的留存率麸祷。極光推送客戶端支持 Android, iOS 兩個(gè)平臺(tái)澎怒。
本 iOS SDK 方便開(kāi)發(fā)者基于 JPush 來(lái)快捷地為 iOS App 增加推送功能,減少集成 APNs 需要的工作量阶牍、開(kāi)發(fā)復(fù)雜度喷面。
創(chuàng)建并配置PushConfig.plist文件
調(diào)用代碼
監(jiān)聽(tīng)系統(tǒng)事件,相應(yīng)地調(diào)用 JPush SDK 提供的 API 來(lái)實(shí)現(xiàn)功能走孽。
static NSString *appKey = @""; // 申請(qǐng)應(yīng)用成功以后官方會(huì)提供給你
static NSString *channel = @"Publish channel";
static BOOL isProduction = FALSE;
#import "JPUSHService.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 極光推送
// Override point for customization after application launch.
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];
}
// AppKey : 是你在極光推送申請(qǐng)下來(lái)的appKey Jchannel : 可以直接設(shè)置默認(rèn)值即可 Publish channel
[JPUSHService setupWithOption:launchOptions appKey:appKey
channel:channel apsForProduction:isProduction];
return YES;
}
#pragma mark - JPushSDK調(diào)用代碼
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Required
[JPUSHService registerDeviceToken:deviceToken];
}
- (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 {
NSString *alert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"推送消息"
message:alert
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
[application setApplicationIconBadgeNumber:0];
// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
用于綁定Alias的,使用NSString即可
// 用于綁定Alias的,使用NSString即可
[JPUSHService setAlias:self.uName.text callbackSelector:nil object:self];
注冊(cè)接收自定義消息
// application didFinishLaunchingWithOptions
// 獲取iOS的推送內(nèi)容需要在delegate類中注冊(cè)通知并實(shí)現(xiàn)回調(diào)方法惧辈。
// 注冊(cè)接收自定義消息的通知
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
// 實(shí)現(xiàn)回調(diào)方法 networkDidReceiveMessage
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSLog(@"收到了自定義信息");
NSDictionary *userInfo = [notification userInfo];
NSString *content = [userInfo valueForKey:@"content"]; // 獲取推送的內(nèi)容
NSDictionary *extras = [userInfo valueForKey:@"extras"]; // 獲取用戶自定義參數(shù)
NSLog(@"extras = %@",extras);
NSLog(@"content = %@",content);
// 建立本地通知,如果程序在后臺(tái)的時(shí)候也會(huì)收到推送通知一樣的消息。也可以判斷在程序在前臺(tái)的時(shí)候做一些特別的操作磕瓷。
if ([content isEqual: @"updatelocation2"]) {
// 立即上傳此時(shí)位置
NSLog(@"立即上傳此時(shí)位置");
// 延時(shí)執(zhí)行
[self performSelector:@selector(autoSubmitLocation) withObject:nil afterDelay:1.0f];
}
}