写在前面:
请先使用pod或者官网下载,导入JPushSDK。
安装导入过程请参照JPush官网开发文档。
App内部操作:
注册JPush
在AppDelegate didFinishLaunchingWithOptions 这个方法里面注册JPush。
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// 可以添加自定义categories
// NSSet<UNNotificationCategory *> *categories for iOS10 or later
// NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
BOOL isProduction = YES;
#if DEBUG
isProduction = NO;//开启开发模式。
#endif
[JPUSHService setupWithOption:launchOptions appKey:appKey channel:nil apsForProduction:isProduction];
注册返回
#pragma mark - 注册返回
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[JPUSHService registerDeviceToken:deviceToken];
}
适配iOS10
需要添加如下代码:
需要NSFoundationVersionNumber_iOS_9_x_Max来进行判断
需要导入UserNotifications.framework
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
// 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];
//这里执行点击通知的跳转方法
DLog(@"iOS10 收到前台通知:")
}
completionHandler(UNNotificationPresentationOptionBadge); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}
// iOS 10 Support
- (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];
//这里执行点击通知的跳转方法
DLog(@"iOS10 收到远程通知:");
}
completionHandler(); // 系统要求执行这个方法
}
#endif
需要在AppDelegate头部加上如下代码:
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
消息推送点击
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[JPUSHService handleRemoteNotification:userInfo];
//这下面执行的,是对接收到的aps,进行必要处理
[self gt_application:application didReceiveRemoteNotification:userInfo];
}
// 收到远程推送通知时
- (void)gt_application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSDictionary *aps = [userInfo valueForKey:@"aps"];
NSString *content = [aps valueForKey:@"alert"];
if (application.applicationState == UIApplicationStateInactive){
//启动APP或者换醒APP,以及相关跳转
}
}
以上基本就已经完成了对JPush的配置。
以下是必须要注意的问题
注意
1.如果你的应用开启了BackGround Modes 如图:
第一个API是后台获取(Background Fetch),该API允许开发者在一个周期间隔后进行特定的动作,如获取网络内容、更新程序界面等等。第二个API是远程通知 (Remote Notification),它是一个新特性,它在当新事件发生时利用推送通知(Push Notifications)去告知程序。这两个新特性都是在后台进行的,这样更加有利于多任务执行。
那么你在AppDelegate里面实现的如下方法
将不再执行
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
s
而是要实现:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
[JPUSHService handleRemoteNotification:userInfo];
//对aps的处理,并且要执行回调,刷新后台状态。
completionHandler(UIBackgroundFetchResultNewData);
}