iOS極光推送 點擊推送消息跳轉頁面

iOS極光推送 點擊推送消息跳轉頁面

字數1272 閱讀14273 評論27 喜歡60數據結構

最近在搞極光推送,以前用的百度推送,可是消息延遲的厲害,就換了極光,換就換吧,無所謂反正我不會,因而就開始看極光推送文檔,內心罵着跟百度的文檔詳細程度不能比啊,文檔很短一下子就看完,其實文檔的主要代碼這些推送平臺都同樣,說到這我想吐槽一下,原本覺得推送很容易,實際就是容易,可是被後臺和安卓開發人員弄的我一頭霧水,一陣惱火!剛開始後臺返回的是推送消息是一段JSON數據,其實正確的就應該返回JSON數據,可是後臺推送給個人通知消息,他妹的就是直接能看到數據結構的內容,什麼{aps:"sb123"}這種類型的,讓我無語的難以形容當時的心情,後來他按照安卓開發人員的要求,把通知消息,換成了自定義消息,通知和自定義消息 徹底就是兩碼事,通知消息是不能改變的,而自定義消息就不一樣了,徹底由開發人員來搞了,通知能夠隨時都能收到消息,可是自定義消息就沒那麼隨意了,自定義消息只有程序運行在前臺的時候纔會收到提示,因此我帶着不樂意的心情讓後臺看了iOS的推送文檔,讓他本身琢磨去吧,後來還好他看了文檔終於上道了,搞定了,好了不廢話了,上代碼!
這些代碼不能缺乏:app

- (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 獲取失敗時,系統會回調此方法spa

- (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:@"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];
}

這樣就搞定了。 下面貼出後臺返回的字段,我是根據這些地段判斷跳轉不一樣的頁面。code

屏幕快照 2015-11-24 下午8.50.02.pngorm


下圖是後臺給的接口文檔接口

屏幕快照 2015-11-24 下午9.35.55.png開發


上述代碼可能會有點亂,若有疑問請留言
看了一下太代碼太亂下面上截圖rem

屏幕快照 2015-11-24 下午9.39.05.png文檔

屏幕快照 2015-11-24 下午9.40.08.pngget

屏幕快照 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

其實這樣作是很nice的,上面寫的有時候會出現bug,能夠去試一下

相關文章
相關標籤/搜索