9to5ios
9to5ios

Reputation: 5545

Enable/disable a perticular UILocalNotification and set multiple notification once

NOTE: Need to know how to Set a number of alarm or notification in between two given dates regularly?

I have to create an app where,i have to enable/disable multiple UILocalnotification set in a tableview.' If i choose a date i have to then set time on that perticular date and also need to set the notification before timePlay (5,10,15,20 min prior).

And End date: the date until when the notification plays regularly.

How to set all the notification for a perticular notification ID at once? How to disable a perticular notification?

ALSO PLEASE TELL ME : CAN HOW CAN I SET UILOCALNOTIFICATION using database? I have create a database having

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.

i can set simple notification like this

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

Upvotes: 2

Views: 243

Answers (2)

Hussain Shabbir
Hussain Shabbir

Reputation: 15015

Try scheduling which will enable the notification and use cancel which will be disable the notification:-

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

Upvotes: 3

amit saini
amit saini

Reputation: 96

at time of notification schedule give a unique notification key in notification's userinfo and to cancel notification you can use below code as where

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

Upvotes: 2

Related Questions