集成步驟不說了辅肾,自己看文檔吧:極光推送iOS文檔
新建一個AppDelegate的category
.h實現(xiàn):
#import "AppDelegate.h"
#import "JPUSHService.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h> // iOS10注冊APNs所需頭文件
#endif
@interface AppDelegate (JPush) <JPUSHRegisterDelegate>
- (void)regisJPushWithOptions:(NSDictionary *)launchOptions;
@end
.m實現(xiàn)
#import "AppDelegate+JPush.h"
@implementation AppDelegate (JPush)
- (void)regisJPushWithOptions:(NSDictionary *)launchOptions{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
/*
apsForProduction標(biāo)識當(dāng)前應(yīng)用所使用的APNs證書環(huán)境。
0 (默認值)表示采用的是開發(fā)證書油吭,
1 表示采用生產(chǎn)證書發(fā)布應(yīng)用。
注:此字段的值要與Build Settings的Code Signing配置的證書環(huán)境一致署拟。
*/
[JPUSHService setupWithOption:launchOptions appKey:JPush_AppKey
channel:@"appStore"
apsForProduction:DEBUG?0:1
advertisingIdentifier:@""];
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];
}
#pragma mark - 實現(xiàn)注冊APNs失敗接口
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
#pragma mark - 實現(xiàn)注冊APNs
- (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 {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個方法婉宰,選擇是否提醒用戶,有Badge推穷、Sound心包、Alert三種類型可以選擇設(shè)置
}
#pragma mark - 添加處理APNs通知回調(diào)方法
// iOS 10 之后處理方法
- (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]]) {
[self handleRemoteNotification:userInfo];
}
completionHandler(); // 系統(tǒng)要求執(zhí)行這個方法
}
//ios7-ios10 處理方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[self handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
//處理推送信息
- (void)handleRemoteNotification:(NSDictionary *)remoteInfo
{
[JPUSHService handleRemoteNotification:remoteInfo];
[self setBadge:0];
}
//設(shè)置角標(biāo)
- (void)setBadge:(int)badge
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
[JPUSHService setBadge:badge];
}
@end
用法:
1.AppDelegate中導(dǎo)入頭文件:#import "AppDelegate+JPush.h"
只需要在AppDelegate的application:didFinishLaunchingWithOptions:
中調(diào)用
[self regisJPushWithOptions:launchOptions];
即可.
自己的操作方法在中實現(xiàn)
- (void)handleRemoteNotification:(NSDictionary *)remoteInfo
{
[JPUSHService handleRemoteNotification:remoteInfo];
[self setBadge:0];
}
這樣做的好處是抽出了push的實現(xiàn)讓結(jié)構(gòu)更加清晰