標籤:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1 if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } else { [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)#else categories:nil]; [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)#endif categories:nil]; } [APService setupWithOption:launchOptions];if (launchOptions) { NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];//這個判斷是在程式沒有啟動並執行情況下收到通知,點擊通知跳轉頁面 if (remoteNotification) { NSLog(@"推送訊息==== %@",remoteNotification); [self goToMssageViewControllerWith:remoteNotification]; } }}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ [application registerForRemoteNotifications];}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ [APService registerDeviceToken:deviceToken]; NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);}
// 當 DeviceToken 擷取失敗時,系統會回調此方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ NSLog(@"DeviceToken 擷取失敗,原因:%@",error);}
- 下面的這個方法也很重要,這裡主要處理推送過來的訊息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ NSLog(@"尼瑪的推送訊息呢===%@",userInfo); // 取得 APNs 標準資訊內容,如果沒需要可以不取 NSDictionary *aps = [userInfo valueForKey:@"aps"]; NSString *content = [aps valueForKey:@"alert"]; //推送顯示的內容 NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; NSString *sound = [aps valueForKey:@"sound"]; //播放的聲音 // 取得自訂欄位內容,userInfo就是後台返回的JSON資料,是一個字典 [APService handleRemoteNotification:userInfo]; application.applicationIconBadgeNumber = 0; [self goToMssageViewControllerWith:userInfo]; }
- (void)applicationWillEnterForeground:(UIApplication *)application { [application setApplicationIconBadgeNumber:0]; //清除角標 [application cancelAllLocalNotifications];}
- (void)goToMssageViewControllerWith:(NSDictionary*)msgDic{ //將欄位存入本地,因為要在你要跳轉的頁面用它來判斷,這裡我只介紹跳轉一個頁面, NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults]; [pushJudge setObject:@"push"forKey:@"push"]; [pushJudge synchronize]; NSString * targetStr = [msgDic objectForKey:@"target"]; if ([targetStr isEqualToString:@"notice"]) { MessageVC * VC = [[MessageVC alloc]init]; UINavigationController * Nav = [[UINavigationController alloc]initWithRootViewController:VC];//這裡加導覽列是因為我跳轉的頁面帶導覽列,如果跳轉的頁面不帶導航,那這句話請省去。 [self.window.rootViewController presentViewController:Nav animated:YES completion:nil]; }}
- 下面介紹要跳轉的頁面MessageVC裡面要做什麼處理,其實裡面的代碼也很簡單。看代碼,在viewWillAppear裡面自行建立一個返回按鈕,根據在AppDelegate裡面用NSUserDefaults儲存的欄位做判斷。
-(void)viewWillAppear:(BOOL)animated{ [self.navigationController setNavigationBarHidden:NO animated:YES]; [super viewWillAppear:YES]; NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults]; if([[pushJudge objectForKey:@"push"]isEqualToString:@"push"]) { self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"[email protected]"] style:UIBarButtonItemStylePlain target:self action:@selector(rebackToRootViewAction)]; }else{ self.navigationItem.leftBarButtonItem=nil; }}
- (void)rebackToRootViewAction { NSUserDefaults * pushJudge = [NSUserDefaults standardUserDefaults]; [pushJudge setObject:@""forKey:@"push"]; [pushJudge synchronize];//記得立即同步 [self dismissViewControllerAnimated:YES completion:nil];}
這樣就搞定了。 下面貼出後台返回的欄位,我是根據這些地段判斷跳轉不同的頁面。
螢幕快照 2015-11-24 下午8.50.02.png
是後台給的介面文檔
螢幕快照 2015-11-24 下午9.35.55.png
上述代碼可能會有點亂,如有疑問請留言
看了一下太代碼太亂下面上
螢幕快照 2015-11-24 下午9.39.05.png
螢幕快照 2015-11-24 下午9.40.08.png
螢幕快照 2015-11-24 下午8.58.11.png
螢幕快照 2015-11-24 下午9.40.43.png
上面5個圖裡面的代碼都在AppDelegate.m裡面
下面一個圖是在MessageVC裡面,就是你要跳轉的那個頁面
螢幕快照 2015-11-24 下午9.42.27.png
iOS極光推送 點擊推送訊息跳轉頁面