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

來源:互聯網
上載者:User

標籤:

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

- (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

其實這樣做是很nice的,上面寫的有時候會出現bug,可以去試一下



文/我就叫CC怎麼了(簡書作者)
原文連結:http://www.jianshu.com/p/eaf07c4372a8
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.