启用/禁用特定的UILocalNotification并设置一次多个通知

时间:2014-12-13 07:24:07

标签: objective-c xcode notifications

注意:需要知道如何定期在两个给定日期之间设置一些警报或通知?

我必须创建一个应用程序,我必须在tableview中启用/禁用多个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 个答案:

答案 0 :(得分: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];
  }
}

答案 1 :(得分:2)

通知时间表中的

在通知的userinfo中提供唯一的通知密钥,并取消您可以在代码下方使用的通知

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;
    }
}