1、UILocalNotification相關參數ios
(1)fireDate,執行本地通知的時間;
app
(2)soundName,通知聲音的名稱;iphone
(3)repeatInterval,重複的週期(如kCFCalendarUnitDay,天天通知一次);函數
(4)timeZone,當地時區;測試
(5)applicationIconBadgeNumber,app icon上得數字提示;spa
(6)alertBody,彈出的通知內容;調試
(7)alertAction,點擊動做;code
2、建立UILocalNotificationorm
(1)建立一個本地通知,10秒後彈出通知。ip
//進入後臺,建立一個本地通知 UILocalNotification *notification = [[UILocalNotification alloc] init]; if (notification) { notification.repeatInterval = kCFCalendarUnitDay; notification.timeZone = [NSTimeZone defaultTimeZone]; NSDate *currentDate = [NSDate date]; NSDate *sinceNow = [currentDate dateByAddingTimeInterval:10]; notification.fireDate = sinceNow; notification.applicationIconBadgeNumber = 1; notification.soundName = UILocalNotificationDefaultSoundName; notification.alertBody = @"測試通知"; notification.alertAction = @"打開"; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }
(2)在進入前臺頁面時- (void)applicationDidBecomeActive:,設置appicon的applicationIconBadgeNumber的值爲0;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
3、IOS8(以上)請求受權
緣由是由於在ios8中,設置應用的application badge value須要獲得用戶的許可。使用以下方法諮詢用戶是否許可應用設置application badge value,在application: didFinishLaunchingWithOptions:中加入如下代碼。
//IOS8以上的系統 float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; if (systemVersion >= 8.0) { if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *noteSetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:noteSetting]; } }
備註:在IOS8 -- iphone6調試報錯:
2015-12-14 16:40:50.997 Mannyi1[3932:1764085] Attempting to badge the application icon but haven't received permission from the user to badge the application
緣由是:沒有用戶權限,沒法設置badge的值,奇怪的是權限已設置(待研究)。
4、其餘
觸發通知有兩種狀況:一種是程序沒有運行,另外一種是程序已經運行;已經在運行又分爲兩種狀況:後臺運行觸發和前臺運行觸發。
(1)我先說明一下程序沒有運行的狀況,能夠在application: didFinishLaunchingWithOptions:函數中判斷是否存在UIApplicationLaunchOptionsLocalNotificationKey。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *loc = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if(loc!=nil){
NSDictionary *dic= loc.userInfo//就使本地通知中傳遞過的NSDictionary,咱們能夠獲取其中的參數。
}
}
(2)程序已運行,在後臺運行通知的時間到了,不會主動觸發- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification函數,須要用戶點擊纔會觸發;程序在前臺運行,會出現這樣的一種狀況就是:通知時間到了會觸發函數,用戶在通知之中心點擊通知也會觸發函數。因此須要區別這兩種狀況,用到的方法就是:當前時間與觸發時間比較在很小的範圍內則表示時間到了觸發,反之表示在用戶在通知中心點擊通知觸發例如:
if([[formatter dateFromString:[formatter stringFromDate:[NSDate date]]] timeIntervalSinceDate:notification.fireDate] > 0.5) {
//用戶點擊了,
}else{
//時間到了觸發的
}
如下的例子是判斷激活狀態的,application: didReceiveLocalNotification函數;
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { //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]; [alert show]; } //} }