user7275929
user7275929

Reputation:

How to schedule notifications in advance?

I would like to create a function that recalls an event. 10min, 20min, 1 hours in advance.

How to do ?

-(void)programmer_un_rappel_sans_avance{

    NSDate *fireDate = [picker_date date];

    UILocalNotification *local_rappel_notification = [[UILocalNotification alloc]init];
    local_rappel_notification.fireDate = fireDate;
    local_rappel_notification.alertBody = @"VOUS AVEZ  UN VOL MAINTENANT !!";
    local_rappel_notification.timeZone = [NSTimeZone defaultTimeZone];
    local_rappel_notification.soundName = UILocalNotificationDefaultSoundName;
    local_rappel_notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

    [[UIApplication sharedApplication ]scheduleLocalNotification:local_rappel_notification];
}

Upvotes: 3

Views: 384

Answers (1)

Vishal Sonawane
Vishal Sonawane

Reputation: 2693

First of all from iOS 10 , apple has deprecated use of UILocalNotifications and they have introduced extremely amazing framework for rich notifications called UserNotifications framework. So I ill suggest you to use this framework instead of using deprecated one. This post has explained use of this framework in a very simple way.Though the post describes Swift coding you can get the idea and convert same in Objective-c or you can also follow this tutorial which explains same in both swift and objective-c.

There are several types of notification triggers which you can use:

  1. UNTimeIntervalNotificationTrigger : Enables notification to be sent after the specified time interval. Can repeat the time interval if required.
  2. UNCalendarNotificationTrigger : Notifies user using date components e.g. trigger at 8am. It can also be repeated.
  3. UNLocationNotificationTrigger : Adds ability to trigger when the user enters or exits a particular location.

In your case you can use UNCalendarNotificationTrigger.

EDIT: For your requirement do like this:

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday) fromDate:[NSDate date]];
NSDateComponents *time = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:your_date];
components.hour = time.hour;
components.minute = time.minute - time_you_want_to_fire_notification_before; //ex. if you want to fire notification before 10 mins then -10 from time.minute

and to fire notification use this:

UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

Upvotes: 1

Related Questions