美文网首页第三方SDK集成iOS技术中心
iOS ● 极光推送 点击推送消息跳转页面[转]

iOS ● 极光推送 点击推送消息跳转页面[转]

作者: MyiOS | 来源:发表于2016-08-17 17:24 被阅读253次

特别鸣谢我叫CC怎么了 ,解决了我的问题

- (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];
}

这样就搞定了。下面贴出后台返回的字段,我是根据这些地段判断跳转不同的页面。

屏幕快照 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,可以去试一下


相关文章

  • iOS ● 极光推送 点击推送消息跳转页面[转]

    特别鸣谢我叫CC怎么了 ,解决了我的问题 下面的这个方法也很重要,这里主要处理推送过来的消息 下面介绍要跳转的页面...

  • iOS极光推送 点击推送消息跳转页面

    最近在搞极光推送,之前用的百度推送,但是消息延迟的厉害,就换了极光,换就换吧,无所谓反正我不会,于是就开始看极光推...

  • 2022-05-30

    极光多厂商离线推送,点击只能跳转app首页,不能跳转指定页面 现象:应用集成极光推送,在线和厂商离线都测试好好的。...

  • 【知识总结】(2)远程推送

    推送SDK:极光推送 后台点击推送: iOS 10 以下收到推送点击触发 iOS 10 以上触发: 极光推送中使用...

  • 极光推送跳转到具体详情

    极光推送点击推送跳转到详情,使用这个方法 极光会传递具体信息过来 自己打印看具体信息,然后根据自己需要跳转具体页面

  • iOS 点击推送消息跳转页面

    看标题就应该知道这次的主题是讲点击推送的消息跳转页面的功能,那么有关证书配置及完整的接入推送功能这里就不细说了,不...

  • Android 极光推送点击消息不跳转指定页面

    导致极光推送接受推送消息点击不跳转情况是因为: i.setFlags(Intent.FLAG_ACTIVITY_...

  • 消息推送---跳转到指定页面

    最近在弄友盟推送,点击推送的消息,打开App,不管在哪个页面都要跳转对应的页面。其中在跳转对应的页面这块卡了,在这...

  • Android推送之弹窗显示+推送点击处理

    推送弹窗 兼容8.0 推送点击处理 当点击推送时,会跳转到pendingintent指定的页面。但是页面太多,我们...

  • 消息推送收集的

    1. 可以用的Demo 无法收到点击消息 iOS - 收到远程推送后的页面跳转 http://blog.csdn....

网友评论

本文标题:iOS ● 极光推送 点击推送消息跳转页面[转]

本文链接:https://www.haomeiwen.com/subject/aewmsttx.html