JPush 使用教程
生活随笔
收集整理的這篇文章主要介紹了
JPush 使用教程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
JPush 使用教程
自己使用的一些經(jīng)驗,為了方便直接從這里復(fù)制過去就行。
就當(dāng)做個筆記,防止長時間忘記之后,還需要去官網(wǎng)看文檔。
主要思路: sdk文件 + 三方依賴系統(tǒng)庫 + 頭文件 + 添加代理 + 初始化代碼
1.版本信息
- JPush : 2.2.0
- Xcode : 8.3.3
- iOS : 6.0 +
2.使用步驟
- 導(dǎo)入頭文件
#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及以后版本注冊可以這樣寫,也可以繼續(xù)用之前的注冊方式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// 如需繼續(xù)使用pushConfig.plist文件聲明appKey等配置內(nèi)容,請依舊使用[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"]; //服務(wù)端傳遞的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); // 需要執(zhí)行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設(shè)置
}// 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(); // 系統(tǒng)要求執(zhí)行這個方法
}- (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
- 添加依賴庫文件
3.其他配置
- 在 JPush 官網(wǎng)注冊應(yīng)用,記住對應(yīng)的Appkey,
代碼中注冊的時候使用。
- 證書配置
直接導(dǎo)出調(diào)試推送證書 和 發(fā)布推送證書 的 .12 文件上傳到JPush 官網(wǎng)。
- 開放App的后臺能力 和 推送能力
JPush 官網(wǎng): https://www.jiguang.cn/accounts/login/form
官方iOS SDK 集成指南:https://docs.jiguang.cn/jpush/client/iOS/ios_guide_new/
轉(zhuǎn)載于:https://www.cnblogs.com/xiaoyouPrince/p/7249970.html
總結(jié)
以上是生活随笔為你收集整理的JPush 使用教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Halcon学习之六:获取Image图像
- 下一篇: 【Web API系列教程】1.2 — W