第三方-雷射推送2.1.0,第三方雷射推2.1.0
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:; } }