標籤:
文章來自:http://www.jianshu.com/p/eaf07c4372a8
AppDelegate.m
- (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)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];}
上面5個圖裡面的代碼都在AppDelegate.m裡面
下面一個圖是在MessageVC裡面,就是你要跳轉的那個頁面
其實這樣做是很nice的,上面寫的有時候會出現bug,可以去試一下
文/我就叫CC怎麼了(簡書作者)
原文連結:http://www.jianshu.com/p/eaf07c4372a8
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。
iOS極光推送 點擊推送訊息跳轉頁面