极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能够及时地推送到终端用户手机上,让开发者积极地保持与用户的连接,从而提高用户活跃度、提高应用的留存率。极光推送客户端支持 Android, iOS 两个平台。
本 iOS SDK 方便开发者基于 JPush 来快捷地为 iOS App 增加推送功能,减少集成 APNs 需要的工作量、开发复杂度。
创建并配置PushConfig.plist文件
调用代码
监听系统事件,相应地调用 JPush SDK 提供的 API 来实现功能。
static NSString *appKey = @""; // 申请应用成功以后官方会提供给你
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 : 是你在极光推送申请下来的appKey Jchannel : 可以直接设置默认值即可 Publish channel
[JPUSHService setupWithOption:launchOptions appKey:appKey
channel:channel apsForProduction:isProduction];
return YES;
}
#pragma mark - JPushSDK调用代码
- (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];
注册接收自定义消息
// application didFinishLaunchingWithOptions
// 获取iOS的推送内容需要在delegate类中注册通知并实现回调方法。
// 注册接收自定义消息的通知
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
// 实现回调方法 networkDidReceiveMessage
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSLog(@"收到了自定义信息");
NSDictionary *userInfo = [notification userInfo];
NSString *content = [userInfo valueForKey:@"content"]; // 获取推送的内容
NSDictionary *extras = [userInfo valueForKey:@"extras"]; // 获取用户自定义参数
NSLog(@"extras = %@",extras);
NSLog(@"content = %@",content);
// 建立本地通知,如果程序在后台的时候也会收到推送通知一样的消息。也可以判断在程序在前台的时候做一些特别的操作。
if ([content isEqual: @"updatelocation2"]) {
// 立即上传此时位置
NSLog(@"立即上传此时位置");
// 延时执行
[self performSelector:@selector(autoSubmitLocation) withObject:nil afterDelay:1.0f];
}
}