在 UILocalNotification 中创建周间隔以及周期

Posted

技术标签:

【中文标题】在 UILocalNotification 中创建周间隔以及周期【英文标题】:Create Intervals of weeks in UILocalNotification along with cycle 【发布时间】:2011-11-15 04:40:34 【问题描述】:

我正在制作使用 UILocalNotification 的应用程序。我想知道如何在 UILocalNotification 中留出空白,即如何安排警报 4 周(每天重复或每隔一天重复一次)并关闭 1 周,然后再打开 4 周,然后关闭 1 周等等。这只是一个案例。这些差距是动态的,并在运行时决定。

【问题讨论】:

还在等待答案......现在一周了 【参考方案1】:

您将无法使用repeatInterval,因为您需要一个特殊的重复方案。我认为您必须为希望收到的每一天安排一次本地通知:

前 4 周每天 28 条通知, 在 4 周的第二个周期中,每天 28 条通知, 等等……

一些可能有帮助的代码:

/**
  This method will schedule 28 notifications, each 24 hours exactly for 4 weeks,
  starting from dayOne (first notification will be at dayOne, the second one
  at dayOne + 24 hours..., so be sure to choose the hour you want by setting
  dayOne correctly.
 */
- (void)scheduleLocalNotificationsEachDayFor4WeeksStartingFrom:(NSDate *)dayOne 

  // Schedule notifications for each day during 4 weeks starting at dayOne
  NSMutableArray *notifications = [NSMutableArray array];
  for (int i = 0; i < 28; i++) 
    [notifications addObject:notificationForDay(dayOne, i)];
  
  for (UILocalNotification *notification in notifications) 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
  


UILocalNotification *notificationInSomeDays(NSDate *referenceDate, NSUInteger some) 
  UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];

  // Notification timing
  NSUInteger days = 60*60*24; // number of seconds in a day
  notification.fireDate = [referenceDate dateByAddingTimeInterval:some * days];
  notification.timeZone = [NSTimeZone localTimeZone]; // use local time zone if your reference date is a local one, or choose the appropriate time zone

  // define your notification content...

  return notification;

您可以使用scheduleLocalNotificationsEachDayFor4WeeksStartingFrom: 方法为每 4 周安排您需要的 28 条通知。因此,您现在可以根据需要随时运行它,方法是在您希望启动通知的每 4 周期间的第一天调用它。

当您启动应用程序时,您应该清除所有当前的本地通知并重新安排它们以符合您的要求。特别是,如果应用程序在应该运行通知的 4 周内启动,您必须进行调整。在这种情况下,您将不得不调整建议的 scheduleLocalNotificationsEachDayFor4WeeksStartingFrom 方法以减少计划通知的数量...

【讨论】:

【参考方案2】:

她的意思是说这些 gaos 是动态的..所以如果她在前 4 周设置 28 个通知,在第二个期间设置 28 个通知,依此类推..那么这将跨越每个应用程序 64 个本地通知.. .....这些差距也是动态的,这意味着它可以是任何假设12周/ 1周休息.....然后通过你的方法,她必须为单一药物设置84个通知(超过限制),他们肯定是这种药不止一种……

【讨论】:

我可以看到问题,问题中没有解释。我没有看到任何解决方案,除了考虑用户有时必须启动应用程序以便可以在限制内设置适当的通知(我不知道)。或使用推送通知。顺便说一句,我认为您应该对我的答案写评论,而不是写评论作为答案。

以上是关于在 UILocalNotification 中创建周间隔以及周期的主要内容,如果未能解决你的问题,请参考以下文章

在 Azure Blob 容器中创建两个文件时,如何在 Azure 数据工厂中创建事件触发器?

将 UILocalNotification 同步到 Watch

如何在 Swift 中使用 UILocalNotification

无法在子目录中创建Jupyter Notebook

在存储过程中创建动态表而在 PL/SQL 块表中创建时权限不足[重复]

检测从哪个 UILocalNotification 打开应用程序