配置:
- 注冊極光賬號并于控制臺創(chuàng)建應用,獲取對應應用的AppKey
- 在極光控制臺中的推送設置里上傳對應推送證書
- 使用pod 'JPush', '3.0.2' 導入極光推送SDK
- Xcode設置,target→Capabilities→Push NOtifications→ON
- Xcode設置,target→Capabilities→Background Modes→ON→Remote notifications打鉤
代碼:
請將以下代碼添加到AppDelegate.m引用頭文件的位置。
// 引入JPush功能所需頭文件
#import "JPUSHService.h"
// iOS10注冊APNs所需頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用idfa功能所需要引入的頭文件(可選)
#import <AdSupport/AdSupport.h>
為AppDelegate中添加委派。
@interface AppDelegate ()<JPUSHRegisterDelegate>
@end
在didFinishLaunchingWithOptions中調(diào)用[self initJpushSDK:launchOptions];
// 極光推送
- (void)initJpushSDK:(NSDictionary *)launchOptions
{
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];
[JPUSHService setupWithOption:launchOptions
appKey:@"1aed1329cf7f8c29730ca75c"
channel:@"App Store"
apsForProduction:YES
advertisingIdentifier:nil];
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if (resCode == 0) {
// [USERDEFAULT setValue:registrationID forKey:kJPushRegistrationID];
// [USERDEFAULT synchronize];
}else {
NSLog(@"registrationID獲取失敗,code:%d",resCode);
}
}];
// 獲取自定義消息
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
}
#pragma mark 【 獲取自定義消息內(nèi)容 】
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notification userInfo];
NSLog(@"自定義message:%@",userInfo);
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// 注冊 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
#pragma mark【 JPUSHRegisterDelegate 】
// 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];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設置
NSLog(@"userInfo111:%@\n%@\n%@\n%@",userInfo,userInfo[@"title"],userInfo[@"content"],userInfo[@"extras"]);
}
// 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];
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
// 程序運行時收到通知,先彈出消息框
NSLog(@"程序在前臺");
}else{
// 跳轉(zhuǎn)到指定頁面
// 這里也可以發(fā)送個通知,跳轉(zhuǎn)到指定頁面
[self goToMssageViewControllerWith:userInfo];
}
}
completionHandler(); // 系統(tǒng)要求執(zhí)行這個方法
}
- (void)goToMssageViewControllerWith:(NSDictionary*)userInfo {
// 此處用于處理點擊消息跳轉(zhuǎn)界面的效果
}