iPhone開發之本地通知
本地通知,local notification,用于基于時間行為的通知,比如有關日歷或者todo列表的小應用。另外,應用如果在后臺執行,iOS允許它在受限的時間內運 行,它也會發現本地通知有用。比如,一個應用,在后臺運行,向應用的服務器端獲取消息,當消息到達時,比如下載更新版本的提示消息,通過本地通知機制通知 用戶。
本地通知是UILocalNotification的實例,主要有三類屬性:
- scheduled time,時間周期,用來指定iOS系統發送通知的日期和時間;
- notification type,通知類型,包括警告信息、動作按鈕的標題、應用圖標上的badge(數字標記)和播放的聲音;
- 自定義數據,本地通知可以包含一個dictionary類型的本地數據。
對本地通知的數量限制,iOS最多允許最近本地通知數量是64個,超過限制的本地通知將被iOS忽略。
如果就寫個簡單的定時提醒,是很簡單的,比如這樣:

示例寫的很簡單,啟動應用后,就發出一個定時通知,10秒后啟動。這時按Home鍵退出,一會兒就會提示上圖的提示信息。如果應用不退出則無效。
代碼如下:
UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { NSLog(@">> support local notification"); NSDate *now=[NSDate new]; notification.fireDate=[now addTimeInterval:10]; notification.timeZone=[NSTimeZone defaultTimeZone]; notification.alertBody=@"該去吃晚飯了!"; [[UIApplication sharedApplication] scheduleLocalNotification:notification];
更詳細的代碼見官方文檔:《Scheduling, Registering, and Handling Notifications》,可以設置比如聲音,比如用戶定義數據等。
設置更多本地通知的信息:
- 設置icon上數字。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. ///////////// application.applicationIconBadgeNumber = 0; // Add the view controller’s view to the window and display. [self.window addSubview:viewController.view]; [self.window makeKeyAndVisible]; return YES; }
- 添加通知時間,通知類型,取消通知
#pragma mark – #pragma mark onChageValue -(IBAction)onChangeValue:(id)sender { UISwitch *switch1=(UISwitch *)sender; if (switch1.on) { UILocalNotification *notification=[[UILocalNotification alloc] init]; NSDate *now1=[NSDate date]; notification.timeZone=[NSTimeZone defaultTimeZone]; notification.repeatInterval=NSDayCalendarUnit; notification.applicationIconBadgeNumber = 1; notification.alertAction = NSLocalizedString(@"顯示", nil); switch (switch1.tag) { case 0: { notification.fireDate=[now1 dateByAddingTimeInterval:10]; notification.alertBody=self.myLable1.text; } break; case 1: { notification.fireDate=[now1 dateByAddingTimeInterval:20]; notification.alertBody=self.myLable2.text; } break; case 2: { notification.fireDate=[now1 dateByAddingTimeInterval:30]; notification.alertBody=self.myLable3.text; } break; default: break; } [notification setSoundName:UILocalNotificationDefaultSoundName]; NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat:@"%d",switch1.tag], @"key1", nil]; [notification setUserInfo:dict]; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }else { NSArray *myArray=[[UIApplication sharedApplication] scheduledLocalNotifications]; for (int i=0; i<[myArray count]; i++) { UILocalNotification *myUILocalNotification=[myArray objectAtIndex:i]; if ([[[myUILocalNotification userInfo] objectForKey:@"key1"] intValue]==switch1.tag) { [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification]; } } } }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!