Local Notification(本地通知)

一、简介

在讲本地通知前,先简单介绍一下推送通知的基本概念,以便对后面的内容有更好的理解。
这里说的推送通知和NSNotification是有所区别的,NSNotification是抽象的,不可见的。而推送通知是可见的,能用肉眼看到。
1.作用
可以让不在前台运行的应用程序,告知用户其内部发生了什么事情。

2.推送通知的呈现方式

  • 在屏幕顶部显示一块横幅(显示具体内容)
  • 在屏幕中间弹出一个UIAlertView(显示具体内容)
  • 在锁屏界面显示一块横幅(锁屏状态下,显示具体内容)
  • 更新app图标的数字(说明新内容的数量)
  • 播放音效(提醒作用)

具体是哪种呈现方式,取决于用户的设置。而且用户接收的推送通知,都会展示在“通知中心”。
用户也可以决定是否要开启以下4个功能:

  • 显示App图标数字
  • 播放音效
  • 锁屏显示
  • 显示在“通知中心”

3.推送通知的使用细节

  • 发出推送通知时,如果当前程序正运行在前台,那么推送通知就不会被呈现出来。
  • 点击推送通知后,默认会自动打开发出推送通知的app
  • 不管app是打开还是关闭,推送通知都能如期发出。
    iOS系统提供了2种推送通知:本地推送通知和远程推送通知

二、本地通知(Local Notification)

1.概念
本地通知:顾名思义,就是不需要服务器的支持,即不需要联网就能发出的推送通知。

2.使用场景
常常用来定时提醒用户完成一些任务,比如清理缓存、看定影、玩游戏等。

3.代码实现:下面直接通过代码来解析本地推送通知的发出

- (void)sendLocalNotification {

    // 创建本地通知
    UILocalNotification*noti = [[UILocalNotificationalloc] init];

    // 设置通知属性
    // 通知内容
    noti.alertBody= @"Hello world!";

    // 通知发送时间
    noti.fireDate= [NSDatedateWithTimeIntervalSinceNow:3];

    // 通知时间所属时区——真实开发中必须制定
    noti.timeZone= [NSTimeZonedefaultTimeZone];

    // 重复通知时间--枚举值
    noti.repeatInterval= NSCalendarUnitSecond;

    // 指定时间重复通知
    noti.repeatCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:@"ID"];

    // 指定锁屏界面通知内容
    noti.alertAction = @"锁屏通知信息";

    // 设置点击通知进入程序图片
    noti.alertLaunchImage= @"Default-568h@2x";

    // 设置通知声音
    noti.soundName = @"buyao.wav";

    // 设置应用程序图标数字
    noti.applicationIconBadgeNumber= 2;

    // 设置附加用户信息
   NSDictionary *userInfo = @{                  
    @"name":@"常",          
    @"age":@"25"                   
    };

    noti.userInfo = userInfo;
    // 注册通知
    [[UIApplicationsharedApplication] scheduleLocalNotification:noti];
}

- (void)removeLocalNotification {
    // 移除所有通知
    [[UIApplicationsharedApplication] cancelAllLocalNotifications];

    // 移除指定通知
  //  [[UIApplication sharedApplication] cancelLocalNotification:<#(UILocalNotification *)#>];
}

 

2.接收到通知后处理
// 程序退出后接到通知进入程序调用该方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
    // 注意: 在iOS8中, 必须提前注册通知类型
    #ifdef
   __IPHONE_8_0    
   if([UIDevicecurrentDevice].systemVersion.doubleValue>= 8.0) {
        UIUserNotificationTypetype = UIUserNotificationTypeAlert| UIUserNotificationTypeBadge| UIUserNotificationTypeSound;

        UIUserNotificationSettings*settings = [UIUserNotificationSettingssettingsForTypes:type categories:nil];

        // 注册通知类型     
       [application registerUserNotificationSettings:settings];
 }
#endif

    static int count = 0;
    count++;

    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 40,200, 200);
    label.numberOfLines = 0;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:21];
    label.backgroundColor= [UIColororangeColor];
    label.text= [NSStringstringWithFormat:@"%@", launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]];
    [self.window.rootViewController.viewaddSubview:label];
    returnYES;
}

//接收到本地通知时就会调用
//当程序在前台时, 会自动调用该方法
//当程序在后台时, 只有用户点击了通知才会调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification
*)notification {
    static int count = 0;   
    count++;
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 250,200, 200);
    label.numberOfLines = 0;
    label.textColor = [UIColor whiteColor];
    label.text = [NSString stringWithFormat:@"
%@", notification.userInfo];
    label.font = [UIFont systemFontOfSize:21];
    label.backgroundColor= [UIColorgrayColor];
    [self.window.rootViewController.viewaddSubview:label];
}

4.点击本地推送通知
如果有本地推送通知,当用户点击时,会自动打开app,这里有2种情况:

  • app并没有关闭,一直隐藏在后台
    点击后,让app进入前台,并会调用AppDelegate的下面方法(并非重新启动app)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;

  • app已经被关闭(进程已死)
    启动app,启动完毕会调用AppDelegate下面的方法
// launchOptions参数通过UIApplicationLaunchOptionsLocalNotificationKey取出本地推送通知对象
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

你可能感兴趣的:(Local Notification(本地通知))