Reputation:
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
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:
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