推送通知(Local Remote)

  • NSNotification是抽象的,不可见的

  • 推送通知是可见的(肉眼可见的)

  • 本地推送通知(Local Notification)

  • 远程推送通知(Remote Notification)

  • 可以让不再前台运行的app,告知用户app内部发生了什么事情
    在屏幕顶部显示一块横幅(显示具体内容)
    在屏幕中间弹出一个UIAlertView(显示具体内容)
    在锁屏界面显示一块横幅(锁屏状态下,显示具体内容)
    更新app图标的数字(说明新内容的数量)
    播放音效(提醒作用)

注册推送通知(本地)

  • 本地推送通知:不需要联网就能发出推送通知(不需要服务器的支持)
  • 使用场景:常用来定时提醒用户完成一些任务(清理垃圾 记账 看电影 玩游戏)
#pragma mark - 在AppDelegate中注册用户通知权限
/*
 UIUserNotificationTypeBadge:图标标记
 UIUserNotificationTypeSound:声音
 UIUserNotificationTypeAlert:弹窗
 */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //设置通知的内容和信息
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
    //注册用户权限
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
    return YES;
}
#pragma mark - 创建通知 设置属性(内容,声音等) 将通知添加到调度池
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{  
    //创建本地推送通知
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
    //设置发送通知的时间
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    //发送通知的消息体
    localNotification.alertBody = @"发送通知的消息体";
    //发送通知的声音
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    
    //发送本地推送通知 -> 加入通知的调度池
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
#pragma mark - 推送通知的属性
    //在消息体上添加一行内容
    localNotification.alertTitle = @"标题";
    //iOS10没有效果 iOS10之前在锁屏时显示 滑动以查看后面文字
    localNotification.alertAction = @"锁屏时调用";
    //图标标记的数字 默认值就是0,如果是0,表示没有改变
    localNotification.applicationIconBadgeNumber = 10;

#pragma mark - 本地推送通知携带信息
//本地推送通知携带的信息
    localNotification.userInfo = @{@"content":@"hehe",@"key":@(2)};
//关闭APP的情况下
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 //判断是否有本地通知
    UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    if(localNotification){
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
        label.backgroundColor = [UIColor blueColor];
        label.text = [NSString stringWithFormat:@"%@",localNotification.userInfo];
        label.numberOfLines = 0;
        [self.window.rootViewController.view addSubview:label];
    }
}
//不关闭APP的情况下
//接收到本地通知时调用 ->点击通知,要打开App时调用
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    NSLog(@"%@",notification.userInfo);
   //调试:控制台 UI调试
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
    label.backgroundColor = [UIColor blueColor];
    label.text = [NSString stringWithFormat:@"%@",notification.userInfo];
    label.numberOfLines = 0;
    [self.window.rootViewController.view addSubview:label];
}
#pragma mark - 通知实现页面跳转通过携带信息userInfo
- (IBAction)page1:(id)sender {
    [self postLocalNotificationWithAlertBody:@"page1" andUserInfo:@{@"content":@"session",@"key":@(1)}];
}
//将发送通知的方法抽取出来
-(void)postLocalNotificationWithAlertBody:(NSString *)alertBody andUserInfo:(NSDictionary *)userInfo {
    //创建本地推送通知
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
    //设置发送通知的时间
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    //发送通知的消息体
    localNotification.alertBody = alertBody;
    //发送通知的声音
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    
    
    //本地推送通知携带的信息
    localNotification.userInfo = userInfo;

    //发送本地推送通知 -> 加入通知的调度池
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
if([UIApplication sharedApplication].applicationState == UIApplicationStateActive){
        //程序在前台,不需要跳转
        return;
    }
//-----------------------------------------------------------------------------------
//根据localNotification的信息跳转到不同的页面
-(void)jumpToViewWith:(UILocalNotification *)localNotification {
    //获取通知中的详细信息
    NSDictionary *userInfo = localNotification.userInfo;
    //获取key的值
    NSUInteger index = [userInfo[@"key"] unsignedIntegerValue];
    //获取tabbat控制器
    UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController;
    //跳转控制器
    tabVC.selectedIndex = index;
}
#pragma mark - 注册通知的categories
//设置分类
    localNotification.category = @"category";
//-----------------------------------------------------------------------------------
//iOS8之后添加分类功能
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
    //设置标识符
    category.identifier = @"category";
    //创建按钮
    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
    //按钮的标识符
    action.identifier = @"foreground";
    //按钮的标题
    action.title = @"打开应用";
    //不打开手机 UIUserNotificationActivationModeBackground
    //打开手机 UIUserNotificationActivationModeForeground
    action.activationMode = UIUserNotificationActivationModeForeground;
    
    //创建按钮1
    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
    //按钮的标识符
    action1.identifier = @"background";
    //按钮的标题
    action1.title = @"稍后处理";
    //不打开手机 UIUserNotificationActivationModeBackground
    //打开手机 UIUserNotificationActivationModeForeground
    action1.activationMode = UIUserNotificationActivationModeBackground;
    
    //添加按钮
    [category setActions:@[action,action1] forContext:UIUserNotificationActionContextDefault];
    
    //设置通知的内容和信息
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:[NSSet setWithObject:category]];
#pragma mark - 点击推送通知的按钮时会调用该方法
//点按通知中的按钮时会调用
//identifier :按钮的标识符
//notification:本地推送通知
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{
    if([identifier isEqualToString:@"foreground"]){
        NSLog(@"打开了应用");
    }else if([identifier isEqualToString:@"background"]){
        NSLog(@"稍后处理");
    }
    //让苹果预估方法运行时间 告诉苹果方法执行完毕
    completionHandler();
}
#pragma mark - 添加输入框
//iOS9增加方法 添加输入框
    action.behavior = UIUserNotificationActionBehaviorTextInput;
//-------------------------------------------------------------
//点按通知中的按钮时会调用
//identifier :按钮的标识符
//notification:本地推送通知
//responseInfo:用户在输入框中输入的文字
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{
    //获取用户输入的文字
    NSString *str = responseInfo[UIUserNotificationActionResponseTypedTextKey];
    //将文字发送给服务器 实现立即回复
    NSLog(@"%@",str); 
    //让苹果预估方法运行时间 告诉苹果方法执行完毕
    completionHandler();
}

远程推送

  • 从服务器推送给客户端的通知(需要联网)

  • 远程推送服务,又称APNs(Apple Push Notification Services)

  • 不管用户打开还是关闭app,只要联网,都能收到服务器推送的远程通知

  • 所有的苹果设备,在联网的状态下,都会与苹果的服务器建立长连接

  • UDID手机唯一标识+bundleID应用程序唯一标识 -> deviceToken

远程推送实现

推送通知(Local Remote)_第1张图片
远程推送实现.png
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //1.请求用户授权
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
    //2.获取deviceToken  向苹果发送服务
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    
    return YES;
}
//当苹果加密成功后,返回deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSLog(@"%@",deviceToken);
}
//当苹果加密失败,返回原因
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"%@",error);
}
//写在didFinishLaunchingWithOptions中,程序被杀死时远程推送
if(launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]){
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
        label.text = [NSString stringWithFormat:@"%@",launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
        label.numberOfLines = 0;
        [self.window.rootViewController.view addSubview:label];
    
//接收到了远程推送通知 程序在前台和后台运行时能够接受
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"%@",userInfo);
}

极光推送

推送通知(Local Remote)_第2张图片
极光推送-1.png
推送通知(Local Remote)_第3张图片
极光推送-2.png
推送通知(Local Remote)_第4张图片
极光推送-3.png
推送通知(Local Remote)_第5张图片
导出证书.png
推送通知(Local Remote)_第6张图片
极光推送-4.png
  • https://docs.jiguang.cn/jpush/client/iOS/ios_sdk/

你可能感兴趣的:(推送通知(Local Remote))