如何提前安排通知?

时间:2016-12-15 11:56:22

标签: objective-c xcode

我想创建一个回忆事件的函数。 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];
}

1 个答案:

答案 0 :(得分:1)

首先来自 iOS 10 ,Apple已弃用UILocalNotifications,他们为称为UserNotifications框架的富通知引入了极其惊人的框架。所以我建议您使用此框架而不是使用已弃用的框架。 This帖子以非常简单的方式解释了这个框架的使用。尽管帖子描述了Swift编码,但您可以在Objective-c中获得想法并将其转换为swift,或者也可以按照{{3}在objective-cNSCalendar *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 中解释相同的教程。

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

  1. UNTimeIntervalNotificationTrigger :启用通知发送 在指定的时间间隔之后。如果可以重复时间间隔 必需的。
  2. UNCalendarNotificationTrigger :使用日期组件通知用户 例如在早上8点触发。它也可以重复。
  3. UNLocationNotificationTrigger :添加触发时的功能 用户进入或退出特定位置。
  4. 在您的情况下,您可以使用 UNCalendarNotificationTrigger

    修改 根据您的要求,请执行以下操作:

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

    和点火通知使用此:

    {{1}}