iOS 远程推送客户端展示

本文说一下当手机收到APNS发来的推送消息后的处理


从技术层面分两种情况:

1.APP运行中(前台或后台)收到推送;

2.APP关闭时收到推送。


从用户角度分两种情况:

1.APP在屏幕上时收到推送;

2.APP不在屏幕上时收到推送。


从用户角度出发,我们提供两种展示推送的效果:

1.APP在屏幕上时收到推送,弹出告警框询问用户是否展示;

2.当<span style="font-size:14px;">APP不在屏幕上时收到推送,屏幕上方状态栏处将出现一条通知,用户如果点击那条通知,表明想要看这条推送信息,所以我们将APP打开同时推送信息直接展示出来,不再弹出告警框询问用户是否展示。


[代码实现]

1.运行中收到推送AppDelegate将执行:

//APP运行中收到推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushInfo
{
    if([UIApplication sharedApplication].applicationIconBadgeNumber > 0) {
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    }
    
    //将通知加入队列
    self.pushInfo = pushInfo;
    [self.pushInfoArray addObject:pushInfo];
    
    //应用在Background状态下收到推送
    if (self.isAppRunningBackground) {
        [self showPushPage:[[pushInfo objectForKey:@"aps"] objectForKey:@"alert"]];
    }
    //应用在Active状态下收到推送
    else {
        _alertPush = [[UIAlertView alloc] initWithTitle:@"HighStyle"
                                                message:[[pushInfo objectForKey:@"aps"] objectForKey:@"alert"]
                                               delegate:self cancelButtonTitle:@"取消"
                                      otherButtonTitles:@"显示", nil];
        [_alertPush show];
    }
}

在这两个方法中变更app的前后台状态:

- (void)applicationDidEnterBackground:(UIApplication *)application

- (void)applicationDidBecomeActive:(UIApplication *)application


2.当app关闭时通过点击手机下拉通知列表中的推送消息启动应用:

<span style="font-size:14px;">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //......


    //向苹果发出注册Push设备号请求--如果用户设置同意接收Push通知的话,程序会去苹果注册并获取deviceToken
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    }
    
    //......
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    //区分app的启动方式:直接启动、本地通知启动、远程通知启动
    self.pushInfoArray = [NSMutableArray arrayWithCapacity:1];
    self.pushInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    self.isLaunchForPush = self.pushInfo ? YES : NO;
   
    if (self.isLaunchForPush) {
        HSTabBarController * tabVC = [[HSTabBarController alloc]init];
        self.window.rootViewController = tabVC;
        tabVC.selectedIndex = 0;
        UINavigationController * nav =(UINavigationController *) [tabVC.viewControllers objectAtIndex:0];
        HSTopicViewController * topicVC = [[HSTopicViewController alloc]init];
        HSTopicModel * topicModel = [[HSTopicModel alloc]init];
        topicModel.uid = [self.pushInfo objectForKey:@"pushextr"];
        topicVC.topicModel = topicModel;
        [nav pushViewController:topicVC animated:NO];
        
    } else {
        HSTabBarController *tabBarcontroller = [HSTabBarController new];
        self.window.rootViewController = tabBarcontroller;
    }
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    return YES;
}</span>
这个方法里面展示推送页用的是push的方式,而应用内收到推送展示用的是present的方式,不清楚为什么这里用present展示推送页没有效果。

最后,如何使通知栏推送消息点击后消失?
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:9];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
据说直接将badgeNumber设置为0可能不生效,要先随便设置一个值,再设置为0.


你可能感兴趣的:(iOS 远程推送客户端展示)