iOS推送通知“黄金宝典”

1. App关闭时接收到他推送通知,通过点击推送通知来启动App

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

在程序启动完成后,在此方法中可以得到推送通知的类容,此处又分为远程通知和本地通知的区别。

远程通知

NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

本地通知

UILocalNotification *localNoti = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSDictionary * userInfo = localNoti.userInfo;

这里,为了让主界面先加载完成,一般需要延迟一小段时间后再去处理推送通知,push出相应的响应页面等。

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self didReceiveNotification:userInfo];
            });

2.App正在运行时接收到推送通知

远程通知

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;

本地通知

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;

App状态

  UIApplicationStateActive, // 激活状态,用户正在使用App
    UIApplicationStateInactive, // 不激活状态,用户切换到其他App、按Home键回到桌面、拉下通知中心
    UIApplicationStateBackground // 在后台运行

根据application.applicationState的状态,判断执行哪种动作。

 

3、检测客户端是否打开了接收通知的功能

检测客户端是否打开了通知:
if (IOS8) { //iOS8以上包含iOS8
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];

        if(UIUserNotificationTypeNone != setting.types) {
            NSLog(@"用户打开了通知");
        }else{
            [self showAlert];
        }
    }else{ // ios7 一下
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if(UIRemoteNotificationTypeNone != type){
            NSLog(@"用户打开了通知");
        }else{
            [self showAlert];
        }
    }


//用户没打开接受通知功能给出提示
-(void)showAlert{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"“凤凰云健康”想给您发送推送通知" message:@"“通知”可能包括提醒、声音和图标标记。这些可在“设置”中配置。" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

 

转载于:https://www.cnblogs.com/hcxl/p/8321547.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值