推送

概述

不在前台的应用程序,如果程序内部发生了需要通知用户的事件,推送通知可以告诉用户发生的事情。
通知又叫消息机制,推送通知分为本地推送和远程推送表现形式相同。
总结:不在前台的应用程序告知用户发生了事件

表现形式

  • 顶部横幅/中间提醒(二选一)
  • 锁屏提醒
  • 图标数字
  • 通知中心(屏幕下滑打开)
推送_第1张图片
5种界面形式.png

特点

  1. APP关闭时可以接收并显示通知
  2. APP打开并处于后台可以接收并显示
  3. APP打开并处于前台可以接收但不会显示
  4. 点击通知后,会默认自动打开发出通知的APP

本地推送

概念

不需要联网就可以发出的通知,不需要服务器的支持。本地通知时是由本地应用触发的,它是基于时间行为的通知,例如闹钟定时、待办事项、清理垃圾等的提醒

使用步骤

  1. 创建一个本地通知UILocalNotification
  2. 设置处理通知的时间fireDate
  3. 设置通知的内容、主题、声音、图标数字
  4. 配置通知传递的自定义参数userInfo(非必须步骤)
  5. 调用通知,可以使用scheduleLocalNotification(按照计划调度通知),也可以使用presentLocalNotification(立即发出通知),注意iOS8之后的用户需要申请同意

使用示例

//创建本地通知
UILocalNotification * localNoti = [[UILocalNotification alloc]init];
//设置延迟时间
localNoti.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0f];
//推送的标题
localNoti.alertTitle = @"AAA";
//推送的具体内容
localNoti.alertBody = @"BBB";
//锁屏时的样子
localNoti.alertAction = @"查看";
//音效文件
localNoti.soundName = @"close.wav";
//设置图标数字
localNoti.applicationIconBadgeNumber = 3;
//设置传递的自定义参数
NSMutableDictionary * userInfo = [NSMutableDictionary dictionary];
userInfo[@"username"] = @"Bob";
userInfo[@"userweight"] = @(100);
localNoti.userInfo = userInfo;
//推出通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
//取得没有触发的通知
NSArray * notis = [[UIApplication sharedApplication] scheduledLocalNotifications];
//取消所有通知
[[UIApplication sharedApplication] cancelAllLocalNotifications];

接收消息时的几种情况

1 . APP彻底退出是调用

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

方法来检测进入,当launchOptions为空时是点击app图标进入(正常登陆),当launchOptions不为空时是点击推送进入,通过UIApplicationLaunchOptionsLocalNotificationKey获取通知

2 . APP没有退出是调用

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

方法来检测进入,当application.applicationState == UIApplicationStateActive时,APP在前台,推送收消息中心,否则是APP在后台通过推送点击进入

远程推送

概念

需要联网,在联网的状态下,所有的苹果设备都会和苹果的服务器建立长连接。
长连接的作用(时间校准、系统升级、查找我的iphone等)
好处 传输速度快、数据能保持最新状态

流程

  1. APP向iOS注册远程推送请求,iOS将请求发送到APNs
  2. APNs会返回device token最终交给APP
  3. APP将device token交给APP公司PUSH服务器程序
  4. PUSH服务器程序向APNs发请求
  5. APNs根据PUSH服务器程序的请求推送远程通知到相应的客户的APP
推送_第2张图片
APNs推送流程.png

使用示例

注册远程通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //获得版本号
    float systemVersion = [UIDevice currentDevice].systemVersion.floatValue;
    //如果版本号低于8.0可以直接推送
    if (systemVersion < 8.0) {
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
    } else {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
        [application registerForRemoteNotifications];
    }
    return YES;
}

获得devicetoken

//此方法不是每次都会进入,所以可以用第二个方法保证进入
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"%@",deviceToken);
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
    [application registerForRemoteNotifications];
}

获取失败

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"register error:%@",error.userInfo);
}

你可能感兴趣的:(推送)