我想创建一个回忆事件的函数。 10分钟,20分钟,提前1小时。
怎么办?
-(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];
}
答案 0 :(得分:1)
首先来自 iOS 10 ,Apple已弃用UILocalNotifications
,他们为称为UserNotifications
框架的富通知引入了极其惊人的框架。所以我建议您使用此框架而不是使用已弃用的框架。 This帖子以非常简单的方式解释了这个框架的使用。尽管帖子描述了Swift
编码,但您可以在Objective-c
中获得想法并将其转换为swift
,或者也可以按照{{3}在objective-c
和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
中解释相同的教程。
您可以使用几种类型的通知触发器:
在您的情况下,您可以使用 UNCalendarNotificationTrigger 。
修改强> 根据您的要求,请执行以下操作:
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
和点火通知使用此:
{{1}}