iOS开发之本地通知UILocalNotification
本地通知是UILocalNotification的實(shí)例,主要有三類(lèi)屬性:
- scheduled time:時(shí)間周期,用來(lái)指定iOS系統(tǒng)發(fā)送通知的日期和時(shí)間;
- notification type:通知類(lèi)型,包括警告信息、動(dòng)作按鈕的標(biāo)題、應(yīng)用圖標(biāo)上的badge(數(shù)字標(biāo)記)和播放的聲音;
- 自定義數(shù)據(jù):本地通知可以包含一個(gè)dictionary類(lèi)型的本地?cái)?shù)據(jù)。
iOS對(duì)本地通知的數(shù)量有限制,最多允許最近的本地通知數(shù)量是64個(gè),超過(guò)限制的本地通知將被iOS忽略。
本地通知的使用:注冊(cè)通知,初始化通知,配置通知,添加通知 。
注冊(cè)通知:
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]]; // 初始化本地通知對(duì)象 UILocalNotification *notification = [[UILocalNotification alloc] init]; if (notification) {// 設(shè)置通知的提醒時(shí)間NSDate *currentDate = [NSDate date];notification.timeZone = [NSTimeZone defaultTimeZone]; notification.fireDate = [currentDate dateByAddingTimeInterval:5.0];// 設(shè)置重復(fù)間隔notification.repeatInterval = kCFCalendarUnitDay;// 設(shè)置提醒的文字內(nèi)容notification.alertBody = @"Wake up, man";notification.alertAction = NSLocalizedString(@"起床了", nil);// 通知提示音 使用默認(rèn)的notification.soundName= UILocalNotificationDefaultSoundName;// 設(shè)置應(yīng)用程序右上角的提醒個(gè)數(shù)notification.applicationIconBadgeNumber++;// 設(shè)定通知的userInfo,用來(lái)標(biāo)識(shí)該通知NSMutableDictionary *aUserInfo = [[NSMutableDictionary alloc] init];aUserInfo[kLocalNotificationID] = @"LocalNotificationID";notification.userInfo = aUserInfo;// 將通知添加到系統(tǒng)中[[UIApplication sharedApplication] scheduleLocalNotification:notification]; }接收通知:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:notification.alertBody preferredStyle:UIAlertControllerStyleAlert];[alertController addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil]];[_window.rootViewController presentViewController:alertController animated:YES completion:nil]; }通知的另外一種注冊(cè)方式:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {[application registerUserNotificationSettings:notificationSettings]; }取消本地通知:
//取消某一個(gè)通知NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];//獲取當(dāng)前所有的本地通知if (!notificaitons || notificaitons.count <= 0) {return;}for (UILocalNotification *notify in notificaitons) {if ([[notify.userInfo objectForKey:@"id"] isEqualToString:LOCAL_NOTIFY_SCHEDULE_ID]) {//取消一個(gè)特定的通知[[UIApplication sharedApplication] cancelLocalNotification:notify];break;}}//取消所有的本地通知[[UIApplication sharedApplication] cancelAllLocalNotifications];本地通知的響應(yīng):
如果已經(jīng)注冊(cè)了本地通知,當(dāng)客戶(hù)端響應(yīng)通知時(shí):
1、應(yīng)用程序在后臺(tái)的時(shí)候,本地通知會(huì)給設(shè)備送達(dá)一個(gè)和遠(yuǎn)程通知一樣的提醒,提醒的樣式由用戶(hù)在手機(jī)設(shè)置中設(shè)置;
2、應(yīng)用程序正在運(yùn)行中,則設(shè)備不會(huì)收到提醒,但是會(huì)走應(yīng)用程序delegate中的方法。
如果想實(shí)現(xiàn)程序在后臺(tái)時(shí)候的提醒效果,可以在上面這個(gè)方法中添加相關(guān)代碼,示例代碼:
if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"關(guān)閉" otherButtonTitles:notification.alertAction, nil nil]; [alert show]; }需要注意的是,在情況1中,如果用戶(hù)點(diǎn)擊提醒進(jìn)入應(yīng)用程序,也會(huì)執(zhí)行收到本地通知的回調(diào)方法,這種情況下如果添加了上面那段代碼,則會(huì)出現(xiàn)連續(xù)出現(xiàn)兩次提示,為了解決這個(gè)問(wèn)題,修改代碼如下:
if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) { //判斷應(yīng)用程序當(dāng)前的運(yùn)行狀態(tài),如果是激活狀態(tài),則進(jìn)行提醒,否則不提醒 if (application.applicationState == UIApplicationStateActive) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"關(guān)閉" otherButtonTitles:notification.alertAction, nil nil]; [alert show]; } }總結(jié)
以上是生活随笔為你收集整理的iOS开发之本地通知UILocalNotification的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: iOS开发两个距离较近的按钮同时触发事件
- 下一篇: iOS之实现“摇一摇”与“扫一扫”功能