最近在搞极光推送,之前用的百度推送,但是消息延迟的厉害,就换了极光,换就换吧,无所谓反正我不会,于是就开始看极光推送文档,心里骂着跟百度的文档详细程度不能比啊,文档很短一会儿就看完,其实文档的主要代码这些推送平台都一样,说到这我想吐槽一下,本来以为推送很容易,实际就是容易,但是被后台和安卓开发人员弄的我一头雾水,一阵恼火!刚开始后台返回的是推送消息是一段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:@"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];
}
这样就搞定了。 下面贴出后台返回的字段,我是根据这些地段判断跳转不同的页面。
![](https://i-blog.csdnimg.cn/blog_migrate/c4ae094e621a7498a6959ff3e441e80c.webp?x-image-process=image/format,png)
下图是后台给的接口文档
![](https://i-blog.csdnimg.cn/blog_migrate/02475ee3ea9bd29a7780d0875f60cc19.webp?x-image-process=image/format,png)
上述代码可能会有点乱,如有疑问请留言
看了一下太代码太乱下面上截图
![](https://i-blog.csdnimg.cn/blog_migrate/e4cfc8e86da6b1ab14b08203788ccb5a.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/4fef6e9337b381eca4ad3a6f0b5189b0.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/7839d1b29f9401855f569b27139d4387.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/d65a1dbf6e38d7bc88bd140fbaea35ae.webp?x-image-process=image/format,png)
上面5个图里面的代码都在AppDelegate.m里面
下面一个图是在MessageVC里面,就是你要跳转的那个页面
![](https://i-blog.csdnimg.cn/blog_migrate/918d212437c1abef422afb9858239d41.webp?x-image-process=image/format,png)