本地通知,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];?
??????????? }?
??????? }?
??? }?
}
?
***************Demo**************
#import <Foundation/Foundation.h>
@interface LocalNotifications : NSObject
// 設置本地通知
+ (void)registerLocalNotification:(NSInteger)alertTime;
// 取消當前通知
+ (void)cancelLocalNotificationWithKey:(NSString *)key;
@end
?
#import "LocalNotifications.h"
// 1.如果需要設置多個通知,key就不能寫成宏定義了,需要動態生成
// 2.以便在用戶關閉某個通知時,可以移除對應的本地通知
#define KAlarmLocalNotificationKey @"KAlarmLocalNotificationKey"
?
@implementation LocalNotifications
?
// 設置本地通知
+ (void)registerLocalNotification:(NSInteger)alertTime
{
? ? UILocalNotification *notification = [[UILocalNotification alloc] init];
? ? // 設置觸發通知的時間
? ? NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
?? ?
? ? notification.fireDate = fireDate;
? ? // 時區
? ? notification.timeZone = [NSTimeZone defaultTimeZone];
? ? // 設置重復的間隔
? ? notification.repeatInterval = kCFCalendarUnitSecond;
? ? // 通知內容
? ? notification.alertBody =? @"發現新版本,請前往更新";
? ? notification.applicationIconBadgeNumber = 1;
? ? // 通知被觸發時播放的聲音
? ? notification.soundName = UILocalNotificationDefaultSoundName;
? ? // 通知參數
? ? NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"是否前往更新" forKey:@"key"];
? ? notification.userInfo = userDict;
?? ?
? ? // ios8后,需要添加這個注冊,才能得到授權
? ? if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
? ? ? ? UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
? ? ? ? UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
? ? ? ? [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
? ? ? ? // 通知重復提示的單位,可以是天、周、月
? ? ? ? notification.repeatInterval = NSCalendarUnitDay;
? ? } else {
? ? ? ? // 通知重復提示的單位,可以是天、周、月
? ? ? ? notification.repeatInterval = NSDayCalendarUnit;
? ? }
?? ?
? ? // 執行通知注冊
? ? [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
// 取消某個本地推送通知
+ (void)cancelLocalNotificationWithKey:(NSString *)key
{
? ? // 獲取所有本地通知數組
? ? NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
?? ?
? ? for (UILocalNotification *notification in localNotifications) {
? ? ? ? NSDictionary *userInfo = notification.userInfo;
? ? ? ? if (userInfo) {
? ? ? ? ? ? // 根據設置通知參數時指定的key來獲取通知參數
? ? ? ? ? ? NSString *info = userInfo[key];
?? ? ? ? ? ?
? ? ? ? ? ? // 如果找到需要取消的通知,則取消
? ? ? ? ? ? if (info != nil) {
? ? ? ? ? ? ? ? [[UIApplication sharedApplication] cancelLocalNotification:notification];
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
@end