2013-10-02 36 views
1

我想做出本地通知,每天通知用戶5次並每天重複, 我有一個可變數組,其對象是「hh:mm」,其中小時和分鐘對於GMT + 3鎮,所以我得到當前日期並找到間隔,然後創建通知 的日期,這是我實施的方法。 - 首次應用時區, - 秒如果當前時間之前的時間,所以使它爲第二天。 - 第三次設置當天的通知。 plz幫助我本地通知問題iphone

piece of code

+0

你只需要設置5個不同的不同的通知以所需的輸入定時,並設置重複間隔notif.repeatInterval = NSDayCalendarUnit;這個。 –

+0

我做過了,但是如果發射通知的日期在當前時間之前發生,那麼我會在第二天發作。 – user2768121

+0

它只是因爲你正在測試,不用擔心用戶會準時得到它,因爲他們不會改變時間和所有的東西,所以不介意這個問題。 –

回答

0

與使用此示例代碼,我有安排2通知一個在上午上午07點,一個在晚上18點,並每天重複它和它的作品超細希望你能找到你的解決方案與此使用。

#pragma mark 
#pragma mark - Notification Setup 
-(void)clearNotification 
{ 
[[UIApplication sharedApplication] cancelAllLocalNotifications]; 
} 

-(void)scheduleNotification 
{ 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    NSMutableArray *arrTemp = [APPDELEGATE.userDefaults valueForKey:@"ParsingResponse"];  

    Class cls = NSClassFromString(@"UILocalNotification"); 
    if (cls != nil) { 
     NSDate *now = [NSDate date]; 
     NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
     NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now]; 
     [components setHour:7]; 
     [components setMinute:0]; 
     NSDate *today7am = [calendar dateFromComponents:components]; 
     UILocalNotification *notif = [[cls alloc] init]; 
     notif.fireDate = today7am; 
     notif.timeZone = [NSTimeZone defaultTimeZone]; 
     notif.repeatCalendar = [NSCalendar currentCalendar]; 
     notif.alertBody = [[arrTemp objectAtIndex:0] objectForKey:@"Noti_Morning"]; 
     notif.alertAction = @"Show me"; 
     notif.soundName = UILocalNotificationDefaultSoundName; 
     notif.repeatInterval = NSDayCalendarUnit; 
     NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Morning", @"key", nil]; 
     notif.userInfo = infoDict; 
     [[UIApplication sharedApplication] scheduleLocalNotification:notif]; 
     [notif release]; 


     NSCalendar *calendar2 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
     NSDateComponents *components2 = [calendar2 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now]; 
     [components2 setHour:18]; 
     [components2 setMinute:0]; 
     NSDate *today6pm = [calendar2 dateFromComponents:components2]; 

     UILocalNotification *notif2 = [[cls alloc] init]; 
     notif2.fireDate = today6pm; 
     notif2.timeZone = [NSTimeZone defaultTimeZone]; 
     notif2.repeatCalendar = [NSCalendar currentCalendar]; 
     notif2.alertBody = [[arrTemp objectAtIndex:0] objectForKey:@"Noti_Evening"]; 
     notif2.alertAction = @"Show me"; 
     notif2.soundName = UILocalNotificationDefaultSoundName; 
     notif2.repeatInterval = NSDayCalendarUnit; 
     NSDictionary *infoDict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Evening", @"key", nil]; 
     notif2.userInfo = infoDict2; 
     [[UIApplication sharedApplication] scheduleLocalNotification:notif2]; 
     [notif2 release]; 
    } 
} 
+0

這裏是同樣的問題,我問我的情況,最後經過大量的研究得到了解決方案http://stackoverflow.com/q/18916631/2695503 –

+0

真的很感謝你的幫助謝謝 – user2768121