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