我已經設置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");
}
現在我的問題是當我根據時間設置通知。它正準時收到警報。但是,當我關閉開關並再次打開時,它並沒有得到警報。根據開關位置,有任何方法可以進行開啓和關閉通知。如果有人知道,請幫助我。提前致謝。
燦一次只有1個UILocalNotification,還是動態創建(任何數量)? – skram