ios极光推送 App收到推送消息时,修改BadgeNumber,同时点击状态栏消息以后跳到指定的页面和静默推送

本文介绍了在iOS应用中如何处理极光推送,包括在不同应用状态下接收推送、自动修改BadgeNumber、点击状态栏消息后的页面跳转以及静默推送的处理。详细讲解了在收到远程推送通知时如何更新应用图标上的数字,并在用户读取消息后正确设置Badge值。此外,还展示了如何根据用户点击推送通知进入指定的页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

极光推送生产环境测试需要打包为adhot或者用testflight

在收到远程推送的消息的时候,有以下三种情况需要考虑:

1. 程序在后台运行

2. 程序未运行

3. 程序运行在前台

原则上,应用在收到推送消息时,badge的值是由后台来控制的,但是,目前的大多数公司后端都不会实现这一功能。幸运的是,JPush服务器已经帮我们做了。

我们在JPush后台发布消息时,在可选设置中,设置badge的值为:+1,就可以让app端badge的值自动加1,这样,我们在app端用代码修改badge的值的时候,都需要同时用[APService setBadge:badgeNumber]修改Push后台记录的badge值,这样下次app收到推送消息时,显示的badge才是正确的。

如果要求对于程序在前台运行时,收到的通知也做处理,这时,我们可以将收到的远程推送通知,转换为本地推送通知,同时修改badge值,就可以了。


设置为+1app图标上的数值就会自动加一,当我们读完消息以后再给极光的后台设置badge的数值 [JPUSHService setBadge:@“读完消息以后给极光后台设置的数值”];

 

·收到消息以后跳到指定的页面

// iOS 10 Support

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

NSInteger currentNumber = [UIApplication sharedApplication].applicationIconBadgeNumber;

    if (currentNumber > 0) {

        currentNumber--;

    }

    [UIApplication sharedApplication].applicationIconBadgeNumber = currentNumber;

    [JPUSHService setBadge:currentNumber];

//    //你的逻辑

//    

//        if ([UIApplication sharedApplication].applicationIconBadgeNumber != 0) {

//                //最后把Iconbadge归0

//               [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

//        

//                [JPUSHService setBadge:0];

//            }

    NSDictionary * userInfo = response.notification.request.content.userInfo;

    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

        [JPUSHService handleRemoteNotification:userInfo];

    }

    //completionHandler();  // 系统要求执行这个方法

     if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {//这是在前台

         

         NSLog(@"nihaoo");

         

     }else{

    NSMutableDictionary *pushDic  = [[NSMutableDictionary alloc]init];

    NSDictionary *dic= userInfo [@"aps"];

    [pushDic setValue:[userInfo objectForKey:@"_j_msgid"] forKey:@"_j_msgid"];

    [pushDic setValue:userInfo [@"order_number"] forKey:@"order_number"];

    [pushDic setValue:dic[@"alert"] forKey:@"alert"];

    [pushDic setValue:dic[@"badge"] forKey:@"badge"];

    [pushDic setValue:dic[@"sound"] forKey:@"sound"];

    [pushDic setValue:userInfo[@"name"] forKey:@"name"];

    completionHandler(UIBackgroundFetchResultNewData);

    [self goToMssageViewControllerWith:pushDic];

     }

}

 

- (UIViewController*)topViewController{

 

return [self topViewControllerWithRootViewController:self.window.rootViewController];

 

}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController{

    

    if ([rootViewController isKindOfClass:[UITabBarController class]]) {

        UITabBarController *tabBarController = (UITabBarController *)rootViewController;

        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];

        } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {

            UINavigationController* navigationController = (UINavigationController*)rootViewController;

             return [self topViewControllerWithRootViewController:navigationController.visibleViewController];

            } else if (rootViewController.presentedViewController) {

                 UIViewController* presentedViewController = rootViewController.presentedViewController;

                 return [self topViewControllerWithRootViewController:presentedViewController];

                } else {

                    return rootViewController;

                }

    

}

 

指定的页面

#import "WtPushTestController.h"

 

@interface WtPushTestController ()

 

@end

 

@implementation WtPushTestController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    NSDictionary *dict = [kUserDefault objectForKey:@"msgDic"];

    

    UILabel *label = [[UILabel alloc]init];

    label.text = dict[@"name"];

    [self.view addSubview:label];

    label.center = self.view.center;

    [label sizeToFit];

    

    

    

}

 

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];

    NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];

    if([[pushJudge objectForKey:@"push"]isEqualToString:@"push"]) {

        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"zh3"] style:UIBarButtonItemStylePlain target:self action:@selector(rebackToRootViewAction)];

    }else{

        self.navigationItem.leftBarButtonItem=nil;

    }

}

- (void)rebackToRootViewAction {

    NSUserDefaults * pushJudge = [NSUserDefaults standardUserDefaults];

    [pushJudge setObject:@""forKey:@"push"];

    [pushJudge setObject:@""forKey:@"msgDic"];

    [pushJudge synchronize];

    [self dismissViewControllerAnimated:YES completion:nil];

}

如果我们希望点击状态栏的推送,点击那个消失那个其它的还保留在ios11上可以这样去做

//你的逻辑

    if ([UIApplication sharedApplication].applicationIconBadgeNumber != 0) {

        //最后把Iconbadge归-1

       [UIApplication sharedApplication].applicationIconBadgeNumber = -1;

        //[JPUSHService setBadge:0];

 

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值