gpt4 book ai didi

objective-c - 如何提前安排通知?

转载 作者:搜寻专家 更新时间:2023-10-30 20:24:12 27 4
gpt4 key购买 nike

我想创建一个函数来记忆一个事件。提前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];
}

最佳答案

首先从 iOS 10 开始,苹果已经弃用了 UILocalNotifications 并且他们引入了非常惊人的丰富通知框架,称为 UserNotifications 框架.所以我建议你使用这个框架而不是使用已弃用的框架。 This帖子以非常简单的方式解释了这个框架的使用。虽然帖子描述了 Swift 编码,但您可以理解并在 Objective-c 中进行转换,或者您也可以按照thisswiftobjective-c 中解释相同的教程。

您可以使用多种类型的通知触发器:

  1. UNTimeIntervalNotificationTrigger :允许发送通知在指定的时间间隔之后。可以重复时间间隔,如果必填。
  2. UNCalendarNotificationTrigger : 使用日期组件通知用户例如早上8点触发。也可以重复。
  3. UNLocationNotificationTrigger :添加触发的能力用户进入或离开特定位置。

在您的情况下,您可以使用 UNCalendarNotificationTrigger

编辑:根据您的要求,请执行以下操作:

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* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

关于objective-c - 如何提前安排通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41163602/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com