1.首先创建一个类目
#import "AppDelegate+JPush.h" #import "JPUSHService.h" #import "WJNotifier.h"//依赖文件 static NSString *JIGUANG_PUSH_KEY = @"4cdcd543c073753fb8932246";//key //NSString *_tag = nil; @implementation AppDelegate (JPush) - (void)initJpush:(NSDictionary *)launchOptions{ if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { //可以添加自定义categories [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } else { //categories 必须为nil [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; } // Required //如需兼容旧版本的方式,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化和同时使用pushConfig.plist文件声明appKey等配置内容。 [JPUSHService setupWithOption:launchOptions appKey:JIGUANG_PUSH_KEY channel:@"0" apsForProduction:YES]; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Required [JPUSHService registerDeviceToken:deviceToken]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // Required,For systems with less than or equal to iOS6 [JPUSHService handleRemoteNotification:userInfo]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // IOS 7 Support Required [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); if (application.applicationState == UIApplicationStateActive) { //音效播放在里面 [WJNotifier showNotifer:userInfo[@"aps"][@"alert"]]; } [CoreManager setJPushNumber]; [[NSNotificationCenter defaultCenter]postNotificationName:notice_jpush object:@"1"]; } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //Optional NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); } @end
2.在下面方法里面调用初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
3.在根控制器中监听状态
- (void)dealloc { [self unObserveAllNotifications]; } - (void)viewDidLoad { [super viewDidLoad]; NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; [defaultCenter addObserver:self selector:@selector(networkDidSetup:) name:kJPFNetworkDidSetupNotification object:nil]; [defaultCenter addObserver:self selector:@selector(networkDidClose:) name:kJPFNetworkDidCloseNotification object:nil]; [defaultCenter addObserver:self selector:@selector(networkDidRegister:) name:kJPFNetworkDidRegisterNotification object:nil]; [defaultCenter addObserver:self selector:@selector(networkDidLogin:) name:kJPFNetworkDidLoginNotification object:nil]; [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil]; [defaultCenter addObserver:self selector:@selector(serviceError:) name:kJPFServiceErrorNotification object:nil]; } - (void)unObserveAllNotifications { NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; [defaultCenter removeObserver:self name:kJPFNetworkDidSetupNotification object:nil]; [defaultCenter removeObserver:self name:kJPFNetworkDidCloseNotification object:nil]; [defaultCenter removeObserver:self name:kJPFNetworkDidRegisterNotification object:nil]; [defaultCenter removeObserver:self name:kJPFNetworkDidLoginNotification object:nil]; [defaultCenter removeObserver:self name:kJPFNetworkDidReceiveMessageNotification object:nil]; [defaultCenter removeObserver:self name:kJPFServiceErrorNotification object:nil]; } - (void)networkDidSetup:(NSNotification *)notification { NSLog(@"已连接"); } - (void)networkDidClose:(NSNotification *)notification { } - (void)networkDidRegister:(NSNotification *)notification { NSLog(@"%@", [notification userInfo]); NSLog(@"已注册"); } - (void)networkDidLogin:(NSNotification *)notification { NSLog(@"已登录"); if ([JPUSHService registrationID]) { NSLog(@"get RegistrationID"); //转入别名 [self resetAliasAndTagWithTag:[UserManager sharedUserInfoWithPlist].name]; } } - (void)networkDidReceiveMessage:(NSNotification *)notification { } - (void)serviceError:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSString *error = [userInfo valueForKey:@"error"]; NSLog(@"%@", error); } - (void)resetAliasAndTagWithTag:(NSString *)tag { __autoreleasing NSMutableSet * tags = [NSMutableSet set]; if (tag == nil) { [tags addObject:@""]; }else { [tags addObject:tag]; } //没有直接传入@"" [JPUSHService setTags:[NSSet set] alias:tag callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self]; } - (void)tagsAliasCallback:(int)iResCode tags:(NSSet *)tags alias:(NSString *)alias { NSLog(@"%d",iResCode); NSLog(@"标签设置成功,标签名为%@",alias); switch (iResCode) { case 6002: [self resetAliasAndTagWithTag:[UserManager sharedUserInfoWithPlist].name]; break; default:; } }
时间: 2024-11-05 01:43:04