文章來自:http://www.jianshu.com/p/eaf07c4372a8app
AppDelegate.mspa
- (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]; }
下面是所跳轉頁面中的一些操做,主要是一個返回操做code
-(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:@"02_03f@2x.png"] 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裏面orm
下面一個圖是在MessageVC裏面,就是你要跳轉的那個頁面blog
其實這樣作是很nice的,上面寫的有時候會出現bug,能夠去試一下ip