2014-12-13 46 views
2

注意:需要知道如何定期在两个给定日期之间设置多个警报或通知?启用/禁用特定的UILocalNotification并设置多次通知

我必须创建一个应用程序,我必须在桌面视图中启用/禁用多个UILocalnotification集。 如果我选择了一个日期,我必须在该日期上设置时间,并且还需要在timePlay(5,10,15,20分钟前)之前设置通知。

和结束日期:直到通知经常播放的日期。

如何一次设置所有关于通知ID的通知? 如何禁用相关通知?

也请告诉我:怎样才能使用数据库设置UILOCALNOTIFICATION? 我已创建有

Notification ID //unique id of notification 
Notification Name //notification title 
Time1 //can set five time the notification will show 
Time2(optional) 
Time3(optional) 
Time4(optional) 
Time5(optional) 
Before timePlay//can show the notification before time of notification 1 to 5 above 
Start Date // the date from which the notification start 
End Date //the date when the notification stops. 

我可以设置简单的通知是这样

// UILocalNotification properties: 
// alertBody - the message displayed in the notification 
// alertAction - if notification is displayed as an alert, this is the label of the action button, if not specified, "Launch" will be used 
// soundName - the name of a sound file (AIFF, CAF or WAV) to be played when the notification appears, if not specified, no sound will be played. To use the default sound UILocalNotificationDefaultSoundName can be provided. 
// userInfo - you can pass an NSDictionary object with additional data to be used by our app after the notification fires (optional) 
// fireDate - an NSDate object specifying the date and time for the local notification to be fired (obligatory) 
// timeZone - an NSTimeZone object. If specified, the fireDate is measured against the user's local time zone, if not against universal time 
// repeatCalendar - the calendar (NSCalendar) the system should refer to when it reschedules a repeating notification 
// repeatInterval - the calendar interval (NSCalendarUnit) at which to reschedule the notification, the default is 0, which means don't repeat 
// alertLaunchImage - will be presented when your app is run or summoned from the background 

// Create a new local notification 
UILocalNotification *notif = [[UILocalNotification alloc] init]; 
    notif.alertBody = @"Wake up! Its tuition time!"; 
    notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:60]; // 60 seconds 
    notif.soundName = UILocalNotificationDefaultSoundName; 
    notif.applicationIconBadgeNumber += 1; 

回答

2

在通知时间表时给出通知的用户信息中的唯一通知密钥以及c ancel通知你可以在下面的代码中使用

UIApplication *app = [UIApplication sharedApplication]; 
NSArray *eventArray = [app scheduledLocalNotifications]; 
for (int i=0; i<[eventArray count]; i++) 
{ 
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i]; 
    NSDictionary *userInfoCurrent = oneEvent.userInfo; 
    NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]]; 
    if ([uid isEqualToString:uidtodelete]) 
    { 
     //Cancelling local notification 
     [app cancelLocalNotification:oneEvent]; 
     break; 
    } 
} 
3

尝试调度,这将使该通知,并使用取消将被禁用通知的数据库: -

-(void)enableNotification 
{ 
    [self cancelAlarm]; //clear any previous alarms 
    UILocalNotification *alarm = [[UILocalNotification alloc] init]; 
    alarm.alertBody = @"alert msg"; 
    alarm.fireDate = [NSDate dateWithTimeInterval:alarmDuration sinceDate:startTime]; 
    alarm.soundName = UILocalNotificationDefaultSoundName; 
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:self.name forKey:kTimerNameKey]; 
alarm.userInfo = userInfo; 
    [[UIApplication sharedApplication] scheduleLocalNotification:alarm]; 
} 

-(void)disableNotification{ 
    for (UILocalNotification *notification in [[[UIApplication sharedApplication] scheduledLocalNotifications] copy]){ 
    NSDictionary *userInfo = notification.userInfo; 
    if ([self.name isEqualToString:[userInfo objectForKey:kTimerNameKey]]){ 
    [[UIApplication sharedApplication] cancelLocalNotification:notification]; 
    } 
} 
相关问题