當需要發送一個本地推送的時候,我們需要為其設定fireTime即發送時間,網上好多範例程式碼只是簡單地將一個類似10秒之後的時間設上去,但我們可能更需要在自訂或使用者定義的某個特定的時間發送,其實這也不難,算是OC的知識點了——對常用類之時間類的運用。
首先我們需要一個具體的時間Date,我們就根據這個時間來將其拆分。這個時間通常來自使用者設定的時間。
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm"; NSDate *testDate = [formatter dateFromString:model.testTime]; NSAssert(testDate != nil, @"testDate = nil");
其次,拆分時間需要的兩個非常重要的類:NSCalender類和NSDateComponent類。初始化這兩個變數,為NSDateComponent類指定Units。
//to set the fire date NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar]; NSDateComponents *dateComponents = [calender components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:testDate];
有了component對象,我們就可以自由為其設定時間了。比如這裡我希望七點提醒,就為component的hour對象賦值7即可。然後就能從component得到一個Date對象。
[dateComponents setHour:7]; NSDate *fireDate = [calender dateFromComponents:dateComponents]; NSLog(@"fire date = %@", fireDate);
這樣就實現了設定特定時間的功能。接下來就屬於本地通知的知識了。
聲明一個UILocalNotification對象(注意做錯誤判斷),然後為其設定各種屬性並且調用應用代理將這個通知發出去就好了。
UILocalNotification *noti = [[UILocalNotification alloc] init]; if (noti == nil) { return; } [noti setTimeZone:[NSTimeZone defaultTimeZone]]; noti.fireDate = fireDate; noti.repeatInterval = 0; noti.alertBody = [NSString stringWithFormat:@"%@考試今天%d點就要開始了,地點是%@,你準備好了嗎。", model.testName, dateComponents.hour, model.testLocation]; noti.alertAction = @"View"; noti.soundName = UILocalNotificationDefaultSoundName; noti.applicationIconBadgeNumber += 1; noti.userInfo = @{@"Kind" : @"testBeginLocalNotification"}; [[UIApplication sharedApplication] scheduleLocalNotification:noti];
最後別忘了在AppDelegate中launch一下。
//-----AppDelegate的didFinishLaunchingWithOptions方法中------- //reset the application icon badge number application.applicationIconBadgeNumber = 0; // Handle launching from a notification UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (localNotif) { NSLog(@"Recieved Notification %@",localNotif); }
附上自己應用中得完整代碼供參考錯誤修正 - -
- (void)setUpLocalNotificationWithModel:(TestModel *)model { if (model.shouldRemind == NO) { return; } UILocalNotification *noti = [[UILocalNotification alloc] init]; if (noti == nil) { return; } [noti setTimeZone:[NSTimeZone defaultTimeZone]]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm"; NSDate *testDate = [formatter dateFromString:model.testTime]; NSAssert(testDate != nil, @"testDate = nil"); //to set the fire date NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar]; NSDateComponents *dateComponents = [calender components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:testDate]; [dateComponents setHour:7]; NSDate *fireDate = [calender dateFromComponents:dateComponents]; NSLog(@"fire date = %@", fireDate); noti.fireDate = fireDate; noti.repeatInterval = 0; //to get the hour dateComponents = [calender components:NSCalendarUnitHour fromDate:testDate]; NSLog(@"test date hour = %d", dateComponents.hour); noti.alertBody = [NSString stringWithFormat:@"%@考試今天%d點就要開始了,地點是%@,你準備好了嗎。", model.testName, dateComponents.hour, model.testLocation]; NSLog(@"tip: %@", noti.alertBody); noti.alertAction = @"View"; noti.soundName = UILocalNotificationDefaultSoundName; NSLog(@"notification application icon badge number = %d", noti.applicationIconBadgeNumber); noti.applicationIconBadgeNumber += 1; noti.userInfo = @{@"Kind" : @"testBeginLocalNotification"}; [[UIApplication sharedApplication] scheduleLocalNotification:noti];}