最近在搞极光推送,之前用的百度推送,但是消息延迟的厉害,就换了极光,换就换吧,无所谓反正我不会,于是就开始看极光推送文档,心里骂着跟百度的文档详细程度不能比啊,文档很短一会儿就看完,其实文档的主要代码这些推送平台都一样,说到这我想吐槽一下,本来以为推送很容易,实际就是容易,但是被后台和安卓开发人员弄的我一头雾水,一阵恼火!刚开始后台返回的是推送消息是一段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 下午9.40.08.png](http://upload-images.jianshu.io/upload_images/969236-ff4948a5c51c6166.png?
上面5个图里面的代码都在AppDelegate.m里面
下面一个图是在MessageVC里面,就是你要跳转的那个页面
其实这样做是很nice的,上面写的有时候会出现bug,可以去试一下
极光推送问题集锦 https://community.jiguang.cn/t/jpush/5145/9