首页 文章

在iOS /不规则时间间隔内安排本地通知

提问于
浏览
1

我正在安排本地通知 . 我的问题是,定期通知只能按每小时,每天,每周,每月或每年的间隔进行安排 . 您没有“每2小时”的选项 .

现在,我希望每2或3小时的时间间隔安排一次 . 此外,由于iOS的限制,一次只能安排64个通知 .

请注意,我无法使用推送通知 .

我怎样才能做到这一点?

2 回答

  • 2

    2小时= 60秒* 60分钟* 2 = 7200秒 . 这会有用吗?如果您在以下代码中使用TimeInterval中的7200,这是五秒钟(from Apple website

    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationStringForKey("Hello!", arguments: nil)
    content.body = NSString.localizedUserNotificationStringForKey("Hello_message_body", arguments: nil)
    content.sound = UNNotificationSound.default() // Deliver the notification in five seconds.
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) // Schedule the notification.
    let center = UNUserNotificationCenter.current()
    center.add(request)
    

    EDIT: 请参阅使用UILocalNotification的类似代码

    UILocalNotification* localNotification = [[UILocalNotificationalloc] init]; 
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7200];
    localNotification.alertBody = @"Your alert message";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    
  • 1

    如果您希望每2小时安排一次通知,则必须安排12次通知,相隔2小时,并每天重复这些通知 . 这将允许您每2小时安排一次通知,无限期地重复,而不会超出您可以设置的通知数量 .

相关问题