輕松搞定iOS本地消息推送
首先,我們先要明白一個(gè)概念,這里的本地通知是UILocalNotification類,和系統(tǒng)的NSNotificationCenter通知中心是完全不同的概念。
一、我們可以通過本地通知做什么
通知,實(shí)際上是由IOS系統(tǒng)管理的一個(gè)功能,比如某些后臺應(yīng)用做了某項(xiàng)活動需要我們處理、已經(jīng)退出的應(yīng)用在某個(gè)時(shí)間提醒我們喚起等等,如果注冊了通知,系統(tǒng)都會在通知觸發(fā)時(shí)給我們發(fā)送消息。由此,我們可以通過系統(tǒng)給我們的APP添加通知用戶的功能,并且應(yīng)用非常廣泛。例如,鬧種類應(yīng)用,有按時(shí)簽到相似功能的應(yīng)用。下面,我們就來介紹如何注冊并且設(shè)置一個(gè)本地通知。
二、了解UILocalNotification類
顧名思義,這個(gè)類就是我們需要使用的本地通知類,先來看它的幾個(gè)屬性:
設(shè)置系統(tǒng)發(fā)送通知的時(shí)間(如果是過去的時(shí)間或者0,則會立刻發(fā)起通知)
@property(nonatomic,copy) NSDate *fireDate;
設(shè)置時(shí)間的時(shí)區(qū)
@property(nonatomic,copy) NSTimeZone *timeZone;
設(shè)置周期性通知
@property(nonatomic) NSCalendarUnit repeatInterval;
NSCalendarUnit對象是枚舉,設(shè)定通知的周期
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ū)域檢測通知是否重復(fù)(如果為YES,則沒次進(jìn)去出來都會發(fā)送,否則只發(fā)送一次)
@property(nonatomic,assign) BOOL regionTriggersOnce;
設(shè)置通知的主體內(nèi)容
@property(nonatomic,copy) NSString *alertBody;
是否隱藏滑動啟動按鈕
@property(nonatomic) BOOL hasAction;
設(shè)置滑動打開的提示文字
@property(nonatomic,copy) NSString *alertAction;
設(shè)置點(diǎn)擊通知后啟動的啟動圖片
@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)如下代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //如果已經(jīng)得到授權(quán),就直接添加本地通知,否則申請?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)擊允許或者不允許后,會執(zhí)行如下代理方法,我們把處理邏輯在其中實(shí)現(xiàn)
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ if (notificationSettings.types!=UIUserNotificationTypeNone) { [self addLocalNotification]; } }
添加本地通知的方法:
-(void)addLocalNotification{ //定義本地通知對象 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ī)界面的滑動動作提示 notification.soundName=UILocalNotificationDefaultSoundName;//收到通知時(shí)播放的聲音,默認(rèn)消息聲音 //調(diào)用通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }
實(shí)現(xiàn)了上面三個(gè)步驟,本地通知的發(fā)出和接受基本都已完成,還有一些細(xì)節(jié)我們需要考慮:
應(yīng)用進(jìn)入前臺后,將Icon上的頭標(biāo)清除:
-(void)applicationWillEnterForeground:(UIApplication *)application{ [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];//進(jìn)入前臺取消應(yīng)用消息圖標(biāo) }
當(dāng)不再需要這個(gè)通知時(shí),清除它
[[UIApplication sharedApplication] cancelAllLocalNotifications];
四、獲取通知中的用戶參數(shù)字典
在上面,我們提到了一個(gè)參數(shù)
@property(nonatomic,copy) NSDictionary *userInfo;
我們可以在注冊通知時(shí)將這個(gè)參數(shù)設(shè)置,然后在收到通知時(shí)使用get方法得到,但是這里有兩種情況:
1、如果我們的APP在前臺或者后臺進(jìn)入前臺時(shí)
這個(gè)方法是APP在前臺或者后臺收到通知進(jìn)入前臺時(shí)調(diào)用的方法
2、如果我們的APP在關(guān)閉狀態(tài)
如果是這種情況,我們只能從下面函數(shù)的launchOptions中取到我們想要的參數(shù)
代碼示例如下:
//接收通知參數(shù) UILocalNotification *notification=[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]; NSDictionary *userInfo= notification.userInfo;
本文已被整理到了《iOS推送教程》,歡迎大家學(xué)習(xí)閱讀。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS10添加本地推送(Local Notification)實(shí)例
- iOS10 適配遠(yuǎn)程推送功能實(shí)現(xiàn)代碼
- 使用iOS推送時(shí)警告錯誤的解決方法
- iOS10實(shí)現(xiàn)推送功能時(shí)的注意點(diǎn)和問題總結(jié)
- iOS點(diǎn)擊推送消息跳到應(yīng)用指定頁面方法
- iOS10全新推送功能實(shí)現(xiàn)代碼
- iOS實(shí)現(xiàn)遠(yuǎn)程推送原理及過程
- iOS自定義推送消息提示框
- 解析iOS10中的極光推送消息的適配
- iOS學(xué)習(xí)筆記之遠(yuǎn)程推送、靜默推送與自定義消息推送
相關(guān)文章
iOS 條碼及二維碼掃描(從相冊中讀取條形碼/二維碼)及掃碼過程中遇到的坑
本文主要給大家介紹ios中從手機(jī)相冊中讀取條形碼和二維碼的問題及解決辦法,需要的朋友參考下2017-01-01詳解Swift 利用Opration和OprationQueue來下載網(wǎng)絡(luò)圖片
這篇文章主要介紹了詳解Swift 利用Opration和OprationQueue來下載網(wǎng)絡(luò)圖片的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09iOS自定義UICollectionViewFlowLayout實(shí)現(xiàn)圖片瀏覽效果
這篇文章主要介紹了iOS自定義UICollectionViewFlowLayout實(shí)現(xiàn)圖片瀏覽效果的相關(guān)資料,需要的朋友可以參考下2016-03-03C++ 中exit(),_exit(),return,abort()函數(shù)的區(qū)別
這篇文章主要介紹了C++ 中exit(),_exit(),return,abort()函數(shù)的區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-12-12