推送通知(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<UITouch *> *)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

远程推送实现

远程推送实现.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);
}

极光推送

极光推送-1.png
极光推送-2.png
极光推送-3.png
导出证书.png
极光推送-4.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,372评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,368评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,415评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,157评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,171评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,125评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,028评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,887评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,310评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,533评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,690评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,411评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,004评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,812评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,693评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,577评论 2 353

推荐阅读更多精彩内容

  • 推送通知注意:这里说的推送通知跟NSNotification有所区别NSNotification是抽象的,不可见的...
    醉叶惜秋阅读 1,515评论 0 3
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 6,719评论 10 16
  • 前言 本文是一篇转载文章,在这一篇实用的文章里,你可以按照上面的步骤实现不借助第三方和服务器端,自己给自己的设备发...
    進无尽阅读 1,668评论 6 6
  • 一、推送通知 注意:这里说的推送通知跟NSNotification有所区别NSNotification是抽象的,不...
    Mg明明就是你阅读 1,251评论 0 17
  • 推送通知 注意:这里说的推送通知跟NSNotification有所区别 NSNotification是抽象的,不可...
    iOS开发攻城狮阅读 4,221评论 1 13