美文网首页iOS优秀开发文章
iOS极光推送 通知点击跳转界面处理 角标处理(Badge)

iOS极光推送 通知点击跳转界面处理 角标处理(Badge)

作者: 吃货_X | 来源:发表于2017-02-16 11:09 被阅读719次

    一、处理通知点击跳转界面处理
    1、APP处于关闭状态,我们点击处理的思路是 先打开APP,在跳转到相应的界面 (这儿可以一级一级的跳)。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //处理App关闭状态 跳转界面
    if (launchOptions) {
            NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
            //这个判断是在程序没有运行的情况下收到通知,点击通知跳转页面
            
            if (remoteNotification) {
                NSLog(@"推送消息==== %@",remoteNotification);
                NSString *tag = remoteNotification[@"tag"]; //tag 是我们设置的标记
                if ([tag isEqual:@"1"]) {//根据tag值跳转相应的界面 
                    self.tab.selectedIndex = 3;//打开相应的主页面
                    UINavigationController *na = self.tab.viewControllers[3];//获取打开的导航控制器
                    LoginViewController *log = [[LoginViewController alloc]init];
                    [na pushViewController:log animated:YES]; //推送到相应的界面
    //我这儿是一级界面,如果是二级或三级,那就需要我们再次设置标记
    //[[NSUserDefaults standardUserDefaults]setObject:@"puth" forKey:@"puth"
                 ];
    //当跳转到相应界面是获取 puth值 根据puth判断 是否跳转
    //比如说我现在点击通知是想到 InviteViewController界面,我就在LoginViewController 的 viewWillAppear 获取 puth值 判断puts值 根据判断 跳转
    InviteViewController。
                } else{
                    self.tab.selectedIndex = 1;
                    UINavigationController *na = self.tab.viewControllers[1];
                    InviteViewController *log = [[InviteViewController alloc]init];
                    [na pushViewController:log animated:YES];
                }
            }
        }
    }
    

    2、APP处于后台,我用极光为例。(跳转效果是,以我最后退出时的那个界面跳转下一个界面,点击返回时返回我退出时那个界面)

    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center
     didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
        NSDictionary * userInfo = response.notification.request.content.userInfo;
        UNNotificationRequest *request = response.notification.request; // 收到推送的请求
        UNNotificationContent *content = request.content; // 收到推送的消息内容
        NSNumber *badge = content.badge;  // 推送消息的角标
        if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
            [JPUSHService handleRemoteNotification:userInfo];
            UINavigationController *nav = self.tab.selectedViewController;//获取当前的导航控制器
            LoginViewController *log = [[LoginViewController alloc]init];//跳转相应界面
            [nav pushViewController:log animated:YES];
            NSInteger number = [badge integerValue];
          //角标问题处理我们获取推送内容里的角标 -1 就是当前的角标
            [[UIApplication sharedApplication] setApplicationIconBadgeNumber:number - 1];
            [JPUSHService setBadge:number - 1];//相当于告诉极光服务器我现在的角标是多少
        }
        completionHandler();  // 系统要求执行这个方法
    }
    

    3、APP处于前台,我在这儿的感受是,点击通知他还是会走上面一个方法。只是在这之前他会走极光提供的处于前台的方法,但是只要点击就会走后台的方法(我自己发现)。

    二、角标处理。
    极光他会自动的角标+1,但有时后会发现没有+1,发两个还是1。这儿可能是后台没处理好。
    前面提到了 处理角标-1 就不说了。还有一种需求就是一进APP角标就清空。

    - (void)applicationDidBecomeActive:(UIApplication *)application {
      [application setApplicationIconBadgeNumber:0];   //清除角标
      [JPUSHService setBadge:0];//同样的告诉极光角标为0了
       [application cancelAllLocalNotifications];
    }
    

    有时候我们的APP需要根据角标在系统内提醒用户。比如APP处于关闭状态下,接收到通知,我们通过点击图标进入APP而不是点击通知进入APP的情况下。
    在应用内提醒用户有新消息。

    NSInteger numbe = [UIApplication sharedApplication].applicationIconBadgeNumber;
        if (numbe > 0) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"messageRedHiddenNO" object:nil];
    //这儿是获取应用的角标数量,大于0 就在系统内提示用户。我这儿就是发一个通知。
        }
    

    相关文章

      网友评论

        本文标题:iOS极光推送 通知点击跳转界面处理 角标处理(Badge)

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