2013-10-31 92 views
13

我試圖使用設置捆綁來安排UILocalNotification。在設置中,您可以選擇是否要讓通知每天(每天1次),每2天1次,僅在星期日或從不。iOS UILocalNotification設置爲每天觸發一次,每兩天一次,並且僅在星期日

這裏是我使用的代碼(這是所有在AppDelegate.m):

-(void)defaultsChanged:(NSNotification *)notification { 

     [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

     [[NSUserDefaults standardUserDefaults]synchronize]; 
     NSString *testValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"multi_preference"]; 

     NSLog(@"%@", testValue); 

     NSDate *today = [NSDate date]; 

     NSCalendar* calendar = [NSCalendar currentCalendar]; 
     NSDateComponents* compoNents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:today]; // Get necessary date components 

     [compoNents month];[compoNents day]; 

     NSDictionary *dictToday= [self getDataFromdate : [compoNents day] month:[compoNents month]]; 


     if ([testValue isEqualToString:@"one"]){ 
      UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
      localNotification.fireDate = [[NSDate date]dateByAddingTimeInterval:20]; 
      localNotification.alertAction = @"View"; 
      localNotification.alertBody = [dictToday objectForKey:@"saint_description"]; 
      localNotification.repeatInterval = NSDayCalendarUnit; 
      localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
      localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; 

      } 
     if (testValue==Nil){ 
      UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
      localNotification.fireDate = [[NSDate date]dateByAddingTimeInterval:20]; 
      localNotification.alertAction = @"View"; 
      localNotification.alertBody = [dictToday objectForKey:@"saint_description"]; 
      localNotification.repeatInterval = NSDayCalendarUnit; 
      localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
      localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; 

     } 
     if ([testValue isEqualToString:@"two"]){ 

      UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
      localNotification.fireDate = [[NSDate date]dateByAddingTimeInterval:86401]; 
      localNotification.alertAction = @"View"; 
      localNotification.alertBody = [dictToday objectForKey:@"saint_description"]; 
      localNotification.repeatInterval = NSDayCalendarUnit; 
      localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
      localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; 

     } 

     if ([testValue isEqualToString:@"three"]){ 
     NSDate *today2 = [[NSDate alloc] init]; 
     NSCalendar *gregorian = [[NSCalendar alloc] 
           initWithCalendarIdentifier:NSGregorianCalendar]; 

     // Get the weekday component of the current date 
     NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit 
                  fromDate:today2]; 

     /* 
     Create a date components to represent the number of days to subtract from the current date. 
     The weekday value for Sunday in the Gregorian calendar is 1, so subtract 1 from the number of days to subtract from the date in question. (If today is Sunday, subtract 0 days.) 
     */ 
     NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init]; 
     [componentsToSubtract setDay: 0 - ([weekdayComponents weekday] - 1)]; 

     NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract 
                  toDate:today2 options:0]; 

     /* 
     Optional step: 
     beginningOfWeek now has the same hour, minute, and second as the original date (today). 
     To normalize to midnight, extract the year, month, and day components and create a new date from those components. 
     */ 
     NSDateComponents *components = 
     [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | 
           NSDayCalendarUnit) fromDate: beginningOfWeek]; 
     beginningOfWeek = [gregorian dateFromComponents:components]; 



      UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
      localNotification.fireDate = beginningOfWeek; 
      localNotification.alertAction = @"View"; 
      localNotification.alertBody = [dictToday objectForKey:@"saint_description"]; 
      localNotification.repeatInterval = NSWeekdayCalendarUnit; 
      localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
      localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; 

     } 
    } 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(defaultsChanged:) 
               name:NSUserDefaultsDidChangeNotification 
               object:nil]; 

    UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
    if (locationNotification) { 
     // Set icon badge number to zero 
     application.applicationIconBadgeNumber = 0; 
    } 
return yes; 
} 
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 




    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Saint" 
                message:notification.alertBody 
                delegate:self cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    if (notification.alertBody!=Nil) 
    [alert show]; 


    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; 

    // Set icon badge number to zero 
    application.applicationIconBadgeNumber = 0; 
} 

代碼是否正確的啓動通知,因爲我已經說? 如果不是,它有什麼問題?謝謝!

回答

6

看起來您是在用戶更改首選項時安排通知。但是,您絕不會未預定之前安排的通知,這就是爲什麼您會在與您一天或兩天前重複更改設置的時間完全對應的時間觀察突發通知。

您安排的通知與您打算修改的通知組不同。不幸的是,UILocalNotifications沒有標識符標記。

然而,當您收到defaultsChanged:消息與你重新安排[[UIApplication sharedApplication] cancelAllLocalNotifications];纔可以取消預定所有以前的通知。這將解決您的問題。

同時仔細看看this solution,建議在啓動時取消並重新安排通知,以避免用戶重新安裝應用時發生突發或重複通知。

+0

謝謝,我已經在使用cancelAllLocalNotifications方法,但我不知道爲什麼,我忘了在這裏粘貼它。事情是,現在他們不應該在什麼時候發射。例如,如果我使用localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:20];和localNotification.repeatInterval = NSDayCalendarUnit;它會在20秒後每天發起通知? –

+0

也許你在錯誤的地方使用它? 'cancelAllLocalNotifications'應該是你粘貼方法中的第一件事。 –

+0

我更新了代碼。但是,通知只是在他們應該啓動時才啓動。有什麼想法嗎? –

2

我有兩個想法,

第一:dictToday的價值是什麼?

NSDictionary *dictToday= [self getDataFromdate : [compoNents day] month:[compoNents month]]; 
[dictToday objectForKey:@"saint_description"] 

如果此值爲零,您的通知將不會彈出。

二:檢查您的時區和時區返回:

[NSTimeZone defaultTimeZone] 

你可能是一個+3時區和設置您的localnotification火上負時區,並且永遠不會發生。

相關問題