iOS中使用本地通知为你的APP添加提示用户功能

iOS中使用本地通知为你的APP添加提示用户功能

首先,咱们先要明白一个概念,这里的本地通知是UILocalNotification类,和系统的NSNotificationCenter通知中心是彻底不一样的概念。app

1、咱们能够经过本地通知作什么

通知,其实是由IOS系统管理的一个功能,好比某些后台应用作了某项活动须要咱们处理、已经退出的应用在某个时间提醒咱们唤起等等,若是注册了通知,系统都会在通知触发时给咱们发送消息。由此,咱们能够经过系统给咱们的APP添加通知用户的功能,而且应用很是普遍。例如,闹种类应用,有按时签到类似功能的应用。下面,咱们就来介绍如何注册而且设置一个本地通知。ide

2、了解UILocalNotification类

顾名思义,这个类就是咱们须要使用的本地通知类,先来看它的几个属性:函数

设置系统发送通知的时间(若是是过去的时间或者0,则会马上发起通知)学习

 

@property(nonatomic,copy) NSDate *fireDate;atom

设置时间的时区spa

 

@property(nonatomic,copy) NSTimeZone *timeZone;.net

设置周期性通知设计

 

@property(nonatomic) NSCalendarUnit repeatInterval;3d

NSCalendarUnit对象是枚举,设定通知的周期代理

typedef NS_OPTIONS(NSUInteger, NSCalendarUnit) {
        NSCalendarUnitEra                = kCFCalendarUnitEra,
        NSCalendarUnitYear               = kCFCalendarUnitYear,
        NSCalendarUnitMonth              = kCFCalendarUnitMonth,
        NSCalendarUnitDay                = kCFCalendarUnitDay,
        NSCalendarUnitHour               = kCFCalendarUnitHour,
        NSCalendarUnitMinute             = kCFCalendarUnitMinute,
        NSCalendarUnitSecond             = kCFCalendarUnitSecond,
        NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,
        NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,
        }

 

设置周期性通知参照的日历表

 

@property(nonatomic,copy) NSCalendar *repeatCalendar;

下面这两个函数是IOS8的新功能,在用户进去或者离开某一区域时发送通知

 

@property(nonatomic,copy) CLRegion *region;

设置区域检测通知是否重复(若是为YES,则没次进去出来都会发送,不然只发送一次)

 

@property(nonatomic,assign) BOOL regionTriggersOnce;

 

设置通知的主体内容

 

@property(nonatomic,copy) NSString *alertBody;  

是否隐藏滑动启动按钮

 

@property(nonatomic) BOOL hasAction; 

设置滑动打开的提示文字

 

@property(nonatomic,copy) NSString *alertAction;

设置点击通知后启动的启动图片

 

@property(nonatomic,copy) NSString *alertLaunchImage; 

下面这个方法是IOS8的新方法,是iwatch的接口,通知的短标题

 

@property(nonatomic,copy) NSString *alertTitle;

收到通知时,播放的系统音

 

@property(nonatomic,copy) NSString *soundName; 

设置应用程序Icon头标数字

 

@property(nonatomic) NSInteger applicationIconBadgeNumber; 

用户字典,可用于传递通知消息参数

 

@property(nonatomic,copy) NSDictionary *userInfo; 

 

注意:这个字符串是系统默认的提示音

 

NSString *const UILocalNotificationDefaultSoundName;

 

3、本地通知的设计流程
首先,想让咱们的APP实现本地通知功能,必须获得用户的受权,在Appdelegate中实现以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //若是已经获得受权,就直接添加本地通知,不然申请询问受权
    if ([[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) {
        [self addLocalNotification];
    }else{
        [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound  categories:nil]];
    }
    return YES;
}

 

当用户点击容许或者不容许后,会执行以下代理方法,咱们把处理逻辑在其中实现

-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
    if (notificationSettings.types!=UIUserNotificationTypeNone) {
        [self addLocalNotification];
    }
}

 

添加本地通知的方法:

-(void)addLocalNotification{
    //定义本地通知对象
    UILocalNotification *notification=[[UILocalNotification alloc]init];
    //设置调用时间
    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:0];//当即触发
    //设置通知属性
    notification.alertBody=@"HELLO,我是本地通知哦!"; //通知主体
    notification.applicationIconBadgeNumber=1;//应用程序图标右上角显示的消息数
    notification.alertAction=@"打开应用"; //待机界面的滑动动做提示 
    notification.soundName=UILocalNotificationDefaultSoundName;//收到通知时播放的声音,默认消息声音
    //调用通知
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

 

实现了上面三个步骤,本地通知的发出和接受基本都已完成,还有一些细节咱们须要考虑:

应用进入前台后,将Icon上的头标清除:

-(void)applicationWillEnterForeground:(UIApplication *)application{
    [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];//进入前台取消应用消息图标
}

 

当再也不须要这个通知时,清除它

 [[UIApplication sharedApplication] cancelAllLocalNotifications];

 

4、获取通知中的用户参数字典

在上面,咱们提到了一个参数

@property(nonatomic,copyNSDictionary *userInfo; 

咱们能够在注册通知时将这个参数设置,而后在收到通知时使用get方法获得,可是这里有两种状况:

一、若是咱们的APP在前台或者后台进入前台时

 

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

这个方法是APP在前台或者后台收到通知进入前台时调用的方法

二、若是咱们的APP在关闭状态

若是是这种状况,咱们只能从下面函数的launchOptions中取到咱们想要的参数

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

代码示例以下:

 //接收通知参数   
  UILocalNotification *notification=[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    NSDictionary *userInfo= notification.userInfo;

 

 

疏漏之处 欢迎指正

学习使用 欢迎转载

 

专一技术,热爱生活,交流技术,也作朋友。

——珲少 QQ群:203317592

相关文章
相关标签/搜索