從iOS8開始,通知被加入了新的特性风罩。簡單地說舵稠,從現(xiàn)在開始,當(dāng)一個通知被展示時廓俭,開發(fā)者可以指定用戶可觸發(fā)的具體的動作(actions),而且甚至不用啟動App也可以處理這個通知研乒。
有幾種方式來提示用戶一個通知,接下來會展示所有支持的通知類型雹熬。正如你已經(jīng)了解的谣膳,你可以指定通知類型為他們中的幾個或者所有。
- Alert or Banner:通知可以用alert或者banner來顯示烈菌,這取決于用戶在設(shè)置中得選擇。他們都應(yīng)當(dāng)包含通知的消息(當(dāng)然是可以本地化的)芽世。
- 聲音(Sound):當(dāng)一個通知被送達(dá)時,你可以‘告訴’iOS播放一段自定義或者系統(tǒng)默認(rèn)的聲音荠割。因為用戶不會一直看著設(shè)備旺矾,被展示的通知有可能會被忽略,所以聲音顯得很有用箕宙。但是,對于不重要的通知火鼻,聲音不應(yīng)該被使用。
- Badge:當(dāng)通知到達(dá)時魁索,一個badge數(shù)字會在App的圖標(biāo)上顯示盼铁。當(dāng)一個通知到達(dá)時,badge數(shù)字必增加1饶火,當(dāng)通知被處理后badge數(shù)字減1。當(dāng)badge數(shù)字不為0或者為0当辐,iOS會顯示或者隱藏badge。
在iOS8之后缘揪,以前的本地推送寫法可能會出錯义桂,接收不到推送的信息,
如果出現(xiàn)以下信息:
1 Attempting to schedule a local notification
2 with an alert but haven't received permission from the user to display alerts
3 with a sound but haven't received permission from the user to play sounds
在IOS8下沒有注冊袖裕,所以需要額外添加對IOS8的注冊方法溉瓶,API中有下面這個方法:
// Registering UIUserNotificationSettings more than once results in previous settings being overwritten.
- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0);
ios8后谤民,需要添加這個注冊攒岛,才能得到授權(quán)
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重復(fù)提示的單位胞锰,可以是天、周顺饮、月
notification.repeatInterval = NSCalendarUnitDay;
} else {
// 通知重復(fù)提示的單位凌那,可以是天、周帽蝶、月
notification.repeatInterval = NSDayCalendarUnit;
}
本地通知回調(diào)函數(shù),當(dāng)應(yīng)用程序在前臺時調(diào)用,此方法在AppDelegate中
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
在不需要再推時佃乘,可以取消推送cancelLocalNotificationWithKey: