JPush 使用教程

自己使用的一些经验,为了方便直接从这里复制过去就行。
就当做个笔记,防止长时间忘记之后,还需要去官网看文档。

主要思路: sdk文件 + 三方依赖系统库 + 头文件 + 添加代理 + 初始化代码

1.版本信息

  • JPush : 2.2.0
  • Xcode : 8.3.3
  • iOS : 6.0 +

2.使用步骤

  • 导入头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
  • 添加如下代码(支持版本为 iOS 6.0+)
#pragma mark -- JPush/**注册apns*/
- (void)registerAPNSWithOptions:(NSDictionary *)launchOptions{//Required//notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;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];// Required// init Push// notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil// 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。[JPUSHService setupWithOption:launchOptions appKey:@"2c6034060b406cfe94d4e2e2"channel:@"App Store"apsForProduction:YESadvertisingIdentifier:nil];NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];}
/**极光推送消息处理@param notification 极光推送通知*/
- (void)networkDidReceiveMessage:(NSNotification *)notification {NSDictionary * userInfo = [notification userInfo];NSString *content = [userInfo valueForKey:@"content"];NSDictionary *extras = [userInfo valueForKey:@"extras"];NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服务端传递的Extras附加字段,key是自己定义的}- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {/// Required - 注册 DeviceToken[JPUSHService registerDeviceToken:deviceToken];
}#pragma mark- JPUSHRegisterDelegate// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {// RequiredNSDictionary * userInfo = notification.request.content.userInfo;if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[JPUSHService handleRemoteNotification:userInfo];}completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {// RequiredNSDictionary * userInfo = response.notification.request.content.userInfo;if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[JPUSHService handleRemoteNotification:userInfo];}completionHandler();  // 系统要求执行这个方法
}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {NSString *content = [userInfo valueForKey:@"content"];NSDictionary *extras = [userInfo valueForKey:@"extras"];NSString *customizeField1 = [extras valueForKey:@"customizeField1"];NSLog(@"content --- %@",content);NSLog(@"extras --- %@",extras);NSLog(@"customizeField1 --- %@",customizeField1);// Required, iOS 7 Support[JPUSHService handleRemoteNotification:userInfo];completionHandler(UIBackgroundFetchResultNewData);
}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {// Required,For systems with less than or equal to iOS6[JPUSHService handleRemoteNotification:userInfo];
}#pragma mark -- JPush
  • 添加依赖库文件

JPush 使用教程-编程之家

3.其他配置

  • 在 JPush 官网注册应用,记住对应的Appkey,

代码中注册的时候使用。

JPush 使用教程-编程之家

  • 证书配置

直接导出调试推送证书 和 发布推送证书 的 .12 文件上传到JPush 官网。

JPush 使用教程-编程之家

  • 开放App的后台能力 和 推送能力

JPush 使用教程-编程之家

JPush 使用教程-编程之家

JPush 官网: https://www.jiguang.cn/accounts/login/form

官方iOS SDK 集成指南:https://docs.jiguang.cn/jpush/client/iOS/ios_guide_new/

转载于:https://www.cnblogs.com/xiaoyouPrince/p/7249970.html