首页 > 代码库 > 极光推送的封装
极光推送的封装
集成步骤不说了,自己看文档吧:极光推送iOS文档
直接上代码了:
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface JPushManager : NSObject +(JPushManager *)shareJPushManager; // 在应用启动的时候调用 - (void)cdm_setupWithOption:(NSDictionary *)launchingOption appKey:(NSString *)appKey channel:(NSString *)channel apsForProduction:(BOOL)isProduction advertisingIdentifier:(NSString *)advertisingId; // 在appdelegate注册设备处调用 - (void)cdm_registerDeviceToken:(NSData *)deviceToken; //设置角标 - (void)cdm_setBadge:(int)badge; //获取注册ID - (void)cdm_getRegisterIDCallBack:(void(^)(NSString *registerID))completionHandler; //处理推送信息 - (void)cdm_handleRemoteNotification:(NSDictionary *)remoteInfo; @end
实现:
// // JPushManager.m // JPushManager // // Created by Doman on 17/3/31. // Copyright ? 2017年 doman. All rights reserved. // #import "JPushManager.h" #import "JPUSHService.h" // iOS10注册APNs所需头文件 #ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif @interface JPushManager ()<JPUSHRegisterDelegate> @end @implementation JPushManager + (JPushManager *)shareJPushManager { static JPushManager * JPushTool = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ JPushTool = [[JPushManager alloc] init]; }); return JPushTool; } // 在应用启动的时候调用 - (void)cdm_setupWithOption:(NSDictionary *)launchingOption appKey:(NSString *)appKey channel:(NSString *)channel apsForProduction:(BOOL)isProduction advertisingIdentifier:(NSString *)advertisingId { 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]; [JPUSHService setupWithOption:launchingOption appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:nil]; return; } // 在appdelegate注册设备处调用 - (void)cdm_registerDeviceToken:(NSData *)deviceToken { [JPUSHService registerDeviceToken:deviceToken]; return; } //设置角标 - (void)cdm_setBadge:(int)badge { [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge]; [JPUSHService setBadge:badge]; } //获取注册ID - (void)cdm_getRegisterIDCallBack:(void(^)(NSString *registerID))completionHandler { [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) { // if(resCode == 0){ // NSLog(@"registrationID获取成功:%@",registrationID); // } // else{ // NSLog(@"registrationID获取失败,code:%d",resCode); // } if (resCode == 0) { NSLog(@"registrationID获取成功:%@",registrationID); completionHandler(registrationID); } }]; } //处理推送信息 - (void)cdm_handleRemoteNotification:(NSDictionary *)remoteInfo { [JPUSHService handleRemoteNotification:remoteInfo]; [self cdm_setBadge:0]; } #pragma mark JPUSHRegisterDelegate // iOS 10 Support - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler { // Required NSDictionary * userInfo = notification.request.content.userInfo; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [self cdm_handleRemoteNotification:userInfo]; } completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有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]]) { [self cdm_handleRemoteNotification:userInfo]; } completionHandler(UNNotificationPresentationOptionAlert); // 系统要求执行这个方法 } @end
AppDelegate.h中:
#import <UIKit/UIKit.h> static NSString *appKey = @"你的appKey"; static NSString *channel = @"APP Store"; static BOOL isProduction = YES; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m中:
// // AppDelegate.m // JPushManager // // Created by Doman on 17/3/31. // Copyright ? 2017年 doman. All rights reserved. // #import "AppDelegate.h" #import "JPushManager.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[JPushManager shareJPushManager] cdm_setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:nil]; [[JPushManager shareJPushManager] cdm_getRegisterIDCallBack:^(NSString *registerID) { NSLog(@"----%@",registerID); }]; [[JPushManager shareJPushManager] cdm_setBadge:0]; // Override point for customization after application launch. return YES; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Required - 注册 DeviceToken [[JPushManager shareJPushManager] cdm_registerDeviceToken:deviceToken]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // Required, iOS 7 Support [[JPushManager shareJPushManager] cdm_handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // Required,For systems with less than or equal to iOS6 [[JPushManager shareJPushManager] cdm_handleRemoteNotification:userInfo]; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
Demo地址:https://github.com/domanc/JPushManager.git
极光推送的封装
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。