首先,我們先要明白一個(gè)概念,這里的本地通知是UILocalNotification類,和系統(tǒng)的NSNotificationCenter通知中心是完全不同的概念。
一、我們可以通過本地通知做什么
通知,實(shí)際上是由IOS系統(tǒng)管理的一個(gè)功能,比如某些后臺(tái)應(yīng)用做了某項(xiàng)活動(dòng)需要我們處理、已經(jīng)退出的應(yīng)用在某個(gè)時(shí)間提醒我們喚起等等,如果注冊(cè)了通知,系統(tǒng)都會(huì)在通知觸發(fā)時(shí)給我們發(fā)送消息。由此,我們可以通過系統(tǒng)給我們的APP添加通知用戶的功能,并且應(yīng)用非常廣泛。例如,鬧種類應(yīng)用,有按時(shí)簽到相似功能的應(yīng)用。下面,我們就來介紹如何注冊(cè)并且設(shè)置一個(gè)本地通知。
二、了解UILocalNotification類
顧名思義,這個(gè)類就是我們需要使用的本地通知類,先來看它的幾個(gè)屬性:
設(shè)置系統(tǒng)發(fā)送通知的時(shí)間(如果是過去的時(shí)間或者0,則會(huì)立刻發(fā)起通知)
@property(nonatomic,copy) NSDate *fireDate;
設(shè)置時(shí)間的時(shí)區(qū)
@property(nonatomic,copy) NSTimeZone *timeZone;
設(shè)置周期性通知
@property(nonatomic) NSCalendarUnit repeatInterval;
NSCalendarUnit對(duì)象是枚舉,設(shè)定通知的周期
1
2
3
4
5
6
7
8
9
10
11
|
typedef NS_OPTIONS(NSUInteger, NSCalendarUnit) { NSCalendarUnitEra = kCFCalendarUnitEra, NSCalendarUnitYear = kCFCalendarUnitYear, NSCalendarUnitMonth = kCFCalendarUnitMonth, NSCalendarUnitDay = kCFCalendarUnitDay, NSCalendarUnitHour = kCFCalendarUnitHour, NSCalendarUnitMinute = kCFCalendarUnitMinute, NSCalendarUnitSecond = kCFCalendarUnitSecond, NSCalendarUnitWeekday = kCFCalendarUnitWeekday, NSCalendarUnitWeekdayOrdinal = kCFCalendarUnitWeekdayOrdinal, } |
設(shè)置周期性通知參照的日歷表
@property(nonatomic,copy) NSCalendar *repeatCalendar;
下面這兩個(gè)函數(shù)是IOS8的新功能,在用戶進(jìn)去或者離開某一區(qū)域時(shí)發(fā)送通知
@property(nonatomic,copy) CLRegion *region;
設(shè)置區(qū)域檢測(cè)通知是否重復(fù)(如果為YES,則沒次進(jìn)去出來都會(huì)發(fā)送,否則只發(fā)送一次)
@property(nonatomic,assign) BOOL regionTriggersOnce;
設(shè)置通知的主體內(nèi)容
@property(nonatomic,copy) NSString *alertBody;
是否隱藏滑動(dòng)啟動(dòng)按鈕
@property(nonatomic) BOOL hasAction;
設(shè)置滑動(dòng)打開的提示文字
@property(nonatomic,copy) NSString *alertAction;
設(shè)置點(diǎn)擊通知后啟動(dòng)的啟動(dòng)圖片
@property(nonatomic,copy) NSString *alertLaunchImage;
下面這個(gè)方法是IOS8的新方法,是iwatch的接口,通知的短標(biāo)題
@property(nonatomic,copy) NSString *alertTitle;
收到通知時(shí),播放的系統(tǒng)音
@property(nonatomic,copy) NSString *soundName;
設(shè)置應(yīng)用程序Icon頭標(biāo)數(shù)字
@property(nonatomic) NSInteger applicationIconBadgeNumber;
用戶字典,可用于傳遞通知消息參數(shù)
@property(nonatomic,copy) NSDictionary *userInfo;
注意:這個(gè)字符串是系統(tǒng)默認(rèn)的提示音
NSString *const UILocalNotificationDefaultSoundName;
三、本地通知的設(shè)計(jì)流程
首先,想讓我們的APP實(shí)現(xiàn)本地通知功能,必須得到用戶的授權(quán),在Appdelegate中實(shí)現(xiàn)如下代碼:
1
2
3
4
5
6
7
8
9
10
|
- ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //如果已經(jīng)得到授權(quán),就直接添加本地通知,否則申請(qǐng)?jiān)儐柺跈?quán) if ([[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) { [self addLocalNotification]; } else { [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; } return YES; } |
當(dāng)用戶點(diǎn)擊允許或者不允許后,會(huì)執(zhí)行如下代理方法,我們把處理邏輯在其中實(shí)現(xiàn)
1
2
3
4
5
|
-( void )application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ if (notificationSettings.types!=UIUserNotificationTypeNone) { [self addLocalNotification]; } } |
添加本地通知的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
-( void )addLocalNotification{ //定義本地通知對(duì)象 UILocalNotification *notification=[[UILocalNotification alloc]init]; //設(shè)置調(diào)用時(shí)間 notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:0]; //立即觸發(fā) //設(shè)置通知屬性 notification.alertBody=@ "HELLO,我是本地通知哦!" ; //通知主體 notification.applicationIconBadgeNumber=1; //應(yīng)用程序圖標(biāo)右上角顯示的消息數(shù) notification.alertAction=@ "打開應(yīng)用" ; //待機(jī)界面的滑動(dòng)動(dòng)作提示 notification.soundName=UILocalNotificationDefaultSoundName; //收到通知時(shí)播放的聲音,默認(rèn)消息聲音 //調(diào)用通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } |
實(shí)現(xiàn)了上面三個(gè)步驟,本地通知的發(fā)出和接受基本都已完成,還有一些細(xì)節(jié)我們需要考慮:
應(yīng)用進(jìn)入前臺(tái)后,將Icon上的頭標(biāo)清除:
1
2
3
|
-( void )applicationWillEnterForeground:(UIApplication *)application{ [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0]; //進(jìn)入前臺(tái)取消應(yīng)用消息圖標(biāo) } |
當(dāng)不再需要這個(gè)通知時(shí),清除它
[[UIApplication sharedApplication] cancelAllLocalNotifications];
四、獲取通知中的用戶參數(shù)字典
在上面,我們提到了一個(gè)參數(shù)
@property(nonatomic,copy) NSDictionary *userInfo;
我們可以在注冊(cè)通知時(shí)將這個(gè)參數(shù)設(shè)置,然后在收到通知時(shí)使用get方法得到,但是這里有兩種情況:
1、如果我們的APP在前臺(tái)或者后臺(tái)進(jìn)入前臺(tái)時(shí)
這個(gè)方法是APP在前臺(tái)或者后臺(tái)收到通知進(jìn)入前臺(tái)時(shí)調(diào)用的方法
2、如果我們的APP在關(guān)閉狀態(tài)
如果是這種情況,我們只能從下面函數(shù)的launchOptions中取到我們想要的參數(shù)
代碼示例如下:
1
2
3
|
//接收通知參數(shù) UILocalNotification *notification=[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]; NSDictionary *userInfo= notification.userInfo; |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。