2012-06-09 69 views
0

我已經設置UILocalNotification根據UISWitch位置通過調用按鈕動作設置警報以下方法。如何在iphone中開啓/關閉UILocalNotification?

- (void)scheduleNotificationWithInterval:(int)minutesBefore { 

    NSDateFormatter* theDateFormatter = [[NSDateFormatter alloc] init]; 
    [theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; 
    [theDateFormatter setDateFormat:@"EEEE"]; 
    NSString *currentDay= [theDateFormatter stringFromDate:combo2.datePick.date]; 

    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    if (localNotif == nil) 
     return; 
    localNotif.fireDate = [combo2.datePick.date dateByAddingTimeInterval:-30]; 
    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotif.repeatInterval=NSCalendarCalendarUnit; 
    localNotif.repeatInterval = NSDayCalendarUnit; 

    if (iBOfSwitchAlarm.on) { 

     if([combo1.selectedText isEqualToString:currentDay]) 
     { 

      NSLog(@"In DayCheck"); 
      localNotif.alertBody = @"Alarm Test"; 
      localNotif.alertAction = @"Ok"; 
      localNotif.soundName = UILocalNotificationDefaultSoundName; 

      NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"THere Message for Alarm" forKey:@"Message"]; 
      localNotif.userInfo = infoDict; 

      [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
     } 
    }else{ 
     NSLog(@"SwOff"); 
    } 
} 

我在AppDelegate中編寫下面的代碼。在這裏我也檢查開關位置。

ViewController *obj =[[ViewController alloc]init]; 

    NSString *itemName = [notif.userInfo objectForKey:@"Message"]; 

    if (obj.iBOfSwitchAlarm.on) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alarm Title" message:itemName delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"Continue", nil]; 
     alertView.tag = 1111; 
     [alertView show]; 

     app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber-1; 
    }else{ 
     NSLog(@"SwitchOff"); 
    } 

現在我的問題是當我根據時間設置通知。它正準時收到警報。但是,當我關閉開關並再次打開時,它並沒有得到警報。根據開關位置,有任何方法可以進行開啓和關閉通知。如果有人知道,請幫助我。提前致謝。

+0

燦一次只有1個UILocalNotification,還是動態創建(任何數量)? – skram

回答

4

關閉您的通知,您可以使用[[UIApplication sharedApplication] cancelAllLocalNotifications];或特定[[UIApplication sharedApplication] cancelLocalNotification:<#(UILocalNotification *)#>]; ,然後你將不得不重新安排這樣

[[MKLocalNotificationsScheduler sharedInstance] scheduleNotificationOn:[[NSDate Date] addTimeInterval:60*60*24*1] 
                    text:@"Hi this is notification" 
                   action:@"View" 
                   sound:@"Raising_Sound.mp3" 
                  launchImage:nil 
                   andInfo:nil]; 

在我的情況下,你通知我正在重新安排了一天後

1

如果一次只能有一個UILocalNotification,我會將它聲明爲一個變量,以便您可以隨意訪問它。

如果您存儲UILocalNotification您可以使用-cancelLocalNotification:的組合來將其OFF-scheduleLocalNotification:將其重新ON,同時保持它的性能完好。