最近在搞極光推送,之前用的百度推送,但是消息延遲的厲害,就換了極光,換就換吧,無所謂反正我不會,于是就開始看極光推送文檔,心里罵著跟百度的文檔詳細(xì)程度不能比啊,文檔很短一會兒就看完,其實文檔的主要代碼這些推送平臺都一樣,說到這我想吐槽一下,本來以為推送很容易,實際就是容易,但是被后臺和安卓開發(fā)人員弄的我一頭霧水,一陣惱火!剛開始后臺返回的是推送消息是一段JSON數(shù)據(jù),其實正確的就應(yīng)該返回JSON數(shù)據(jù),但是后臺推送給我的通知消息,他妹的就是直接能看到數(shù)據(jù)結(jié)構(gòu)的內(nèi)容,什么{aps:"sb123"}這種類型的,讓我無語的難以形容當(dāng)時的心情,后來他按照安卓開發(fā)人員的要求,把通知消息,換成了自定義消息,通知和自定義消息 完全就是兩碼事,通知消息是不能改變的,而自定義消息就不同了,完全由開發(fā)人員來搞了,通知可以隨時都能收到消息,但是自定義消息就沒那么隨意了,自定義消息只有程序運行在前臺的時候才會收到提示,所以我?guī)е粯芬獾男那樽尯笈_看了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];
//這個判斷是在程序沒有運行的情況下收到通知,點擊通知跳轉(zhuǎn)頁面
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]);
}
// 當(dāng) DeviceToken 獲取失敗時,系統(tǒng)會回調(diào)此方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"DeviceToken 獲取失敗,原因:%@",error);
}
- 下面的這個方法也很重要,這里主要處理推送過來的消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(@"尼瑪?shù)耐扑拖⒛?==%@",userInfo);
// 取得 APNs 標(biāo)準(zhǔn)信息內(nèi)容,如果沒需要可以不取
NSDictionary *aps = [userInfo valueForKey:@"aps"];
NSString *content = [aps valueForKey:@"alert"]; //推送顯示的內(nèi)容
NSInteger badge = [[aps valueForKey:@"badge"] integerValue];
NSString *sound = [aps valueForKey:@"sound"]; //播放的聲音
// 取得自定義字段內(nèi)容,userInfo就是后臺返回的JSON數(shù)據(jù),是一個字典
[APService handleRemoteNotification:userInfo];
application.applicationIconBadgeNumber = 0;
[self goToMssageViewControllerWith:userInfo];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[application setApplicationIconBadgeNumber:0]; //清除角標(biāo)
[application cancelAllLocalNotifications];
}
- (void)goToMssageViewControllerWith:(NSDictionary*)msgDic{
//將字段存入本地,因為要在你要跳轉(zhuǎn)的頁面用它來判斷,這里我只介紹跳轉(zhuǎn)一個頁面,
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];//這里加導(dǎo)航欄是因為我跳轉(zhuǎn)的頁面帶導(dǎo)航欄,如果跳轉(zhuǎn)的頁面不帶導(dǎo)航,那這句話請省去。
[self.window.rootViewController presentViewController:Nav animated:YES completion:nil];
}
}
- 下面介紹要跳轉(zhuǎn)的頁面MessageVC里面要做什么處理,其實里面的代碼也很簡單。看代碼,在viewWillAppear里面自行創(chuàng)建一個返回按鈕,根據(jù)在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];
}
*這樣就搞定了。
*下面貼出后臺返回的字段,我是根據(jù)這些地段判斷跳轉(zhuǎn)不同的頁面。
下圖是后臺給的接口文檔
上述代碼可能會有點亂,如有疑問請留言
看了一下太代碼太亂下面上截圖
的那個頁面
其實這樣做是很nice的,上面寫的有時候會出現(xiàn)bug,可以去試一下