2012-10-31 33 views
2

每天UILocalNotification如何每天重複UILocalNotification下午5點?以下是我設置自定義時間的代碼。但我想每天通知用戶定製或可能是靜態時間。我正在使用iOS 6.重複下午5時

-(void)scheduleNotification{ 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"h:mm a"]; 
NSDate *dateFromString = [[NSDate alloc] init]; 

dateFromString = [dateFormatter dateFromString:timeStr]; 

UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 

if (localNotif == nil) 
    return; 


localNotif.fireDate = dateFromString; 

localNotif.repeatInterval = kCFCalendarUnitDay; 
localNotif.timeZone = [NSTimeZone defaultTimeZone]; 

// Notification details 
localNotif.alertBody = @"Reminder is set"; 
// Set the action button 
localNotif.alertAction = @"Ok"; 

localNotif.soundName = UILocalNotificationDefaultSoundName; 
localNotif.applicationIconBadgeNumber = 1; 

// Specify custom data for the notification 
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Local Push received while running", @"key", nil]; 
localNotif.userInfo = infoDict; 

// Schedule the notification 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

} 
+0

那麼你需要什麼'localNotif.repeatInterval = kCFCalendarUnitDay;'不能做? – 2012-10-31 10:54:43

+0

它只顯示徽章。警報視圖不顯示。 –

+0

請點擊此鏈接http://stackoverflow.com/questions/11303413/iphone-daily-local-notifications –

回答

7

使用此。它可以幫助你

NSDateFormatter *dat= [[NSDateFormatter alloc]init]; 
[dat setLocale:[NSLocale currentLocale]]; 
[dat setTimeZone:[NSTimeZone systemTimeZone]]; 

//[dat setDateFormat:@"YYYY-MM-dd"];// YYYY-MM-dd hh:mm a 
//NSString *dateM=[dat stringFromDate:datM]; 
//[dat setDateFormat:@"YYYY-MM-dd h:mm a"]; 
NSDate *reminderDate=[NSDate date]; 
reminderDate =[reminderDate dateByAddingTimeInterval:1*24*60*60]; 

UILocalNotification *missingDreamNotify=[[UILocalNotification alloc]init]; 
missingDreamNotify.fireDate=reminderDate; 
missingDreamNotify.timeZone = [NSTimeZone defaultTimeZone]; 
missingDreamNotify.alertBody = @"Reminder is set"; 
missingDreamNotify.alertAction = @"Show me"; 
missingDreamNotify.soundName = UILocalNotificationDefaultSoundName; 
missingDreamNotify.applicationIconBadgeNumber = 1; 
missingDreamNotify.repeatInterval = NSDayCalendarUnit; 

[[UIApplication sharedApplication] scheduleLocalNotification:missingDreamNotify]; 
+2

更好的做法是使用NSDateComponents抵消日期,因爲不是所有的日子都是一樣的長度。 – shim

+0

如何確保這將在下午5點(根據用戶設備時間)觸發? – Hemang

0

這裏是我的解決方案,利用NSCalendar:

// called in didFinishLaunchingWithOptions 
/* 

cancel all local notifications at start up and reset them 
as repeating notifications. 

processDailyNotifications 
    register to system 
    cancel all 
    for i in notifArray: 
     setDailyNotificationAtHour:minute: 

setDailyNotificationAtHour:minute: 
*/ 

- (void)processDailyNotifications 
{ 

    UIApplication *application = [UIApplication sharedApplication]; 
    // are you running on iOS8? 
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) 
    { 
     UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil]; 
     [application registerUserNotificationSettings:settings]; 
    } 
    else // iOS 7 or earlier 
    { 
     UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; 
     [application registerForRemoteNotificationTypes:myTypes]; 
    } 

    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    [@[@{@"hour":@0,@"minute":@58},@{@"hour":@0, @"minute":@59}] enumerateObjectsUsingBlock:^(NSDictionary *item, NSUInteger idx, BOOL *stop) { 
     [self setDailyNotificationAtHour:[[item valueForKey:@"hour"] integerValue] minute:[[item valueForKey:@"minute"] integerValue]]; 
    }]; 
} 

- (void)setDailyNotificationAtHour:(NSInteger)hour minute:(NSInteger)minute 
{ 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSDate *now = [NSDate date]; 
    NSDate *expected = [calendar dateBySettingHour:hour minute:minute second:0 ofDate:now options:NSCalendarMatchStrictly]; 

    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    notification.fireDate = expected; 
    notification.alertAction = @"記錄"; 
    notification.alertBody = @"記錄時間"; 
    notification.soundName = UILocalNotificationDefaultSoundName; 
    notification.applicationIconBadgeNumber = 1; 
    notification.repeatInterval = NSDayCalendarUnit; 
    ALog(@"%ld, %ld, %@", (long)hour, (long)minute, [expected descriptionWithLocale:[NSLocale currentLocale]]); 

    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
} 
相關問題