首先,咱們先要明白一個概念,這裏的本地通知是UILocalNotification類,和系統的NSNotificationCenter通知中心是徹底不一樣的概念。app
通知,其實是由IOS系統管理的一個功能,好比某些後臺應用作了某項活動須要咱們處理、已經退出的應用在某個時間提醒咱們喚起等等,若是註冊了通知,系統都會在通知觸發時給咱們發送消息。由此,咱們能夠經過系統給咱們的APP添加通知用戶的功能,而且應用很是普遍。例如,鬧種類應用,有按時簽到類似功能的應用。下面,咱們就來介紹如何註冊而且設置一個本地通知。ide
顧名思義,這個類就是咱們須要使用的本地通知類,先來看它的幾個屬性:函數
設置系統發送通知的時間(若是是過去的時間或者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;
- (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];
在上面,咱們提到了一個參數
@property(nonatomic,copy) NSDictionary *userInfo;
咱們能夠在註冊通知時將這個參數設置,而後在收到通知時使用get方法獲得,可是這裏有兩種狀況:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
這個方法是APP在前臺或者後臺收到通知進入前臺時調用的方法
若是是這種狀況,咱們只能從下面函數的launchOptions中取到咱們想要的參數
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
代碼示例以下:
//接收通知參數 UILocalNotification *notification=[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]; NSDictionary *userInfo= notification.userInfo;
疏漏之處 歡迎指正
學習使用 歡迎轉載
專一技術,熱愛生活,交流技術,也作朋友。
——琿少 QQ羣:203317592