最近在搞極光推送,之前用的百度推送,但是消息延遲的厲害,就換了極光,換就換吧,無所謂反正我不會,於是就開始看極光推送文檔,心裡罵著跟百度的文檔詳細程度不能比啊,文檔很短一會兒就看完,其實文檔的主要代碼這些推送平台都一樣,說到這我想吐槽一下,本來以為推送很容易,實際就是容易,但是被後台和安卓開發人員弄的我一頭霧水,一陣惱火!剛開始後台返回的是推送消息是一段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];
}
這樣就搞定了。下面貼出後台返回的字段,我是根據這些地段判斷跳轉不同的頁面。
下圖是後台給的接口文檔
上述代碼可能會有點亂,如有疑問請留言
看了一下太代碼太亂下面上截圖
上面5個圖裡面的代碼都在AppDelegate.m裡面
下面一個圖是在MessageVC裡面,就是你要跳轉的那個頁面
其實這樣做是很nice的,上面寫的有時候會出現bug,可以去試一下