簡體   English   中英

在iOS /不規則時間間隔內安排本地通知

[英]Schedule Local Notification in iOS / irregular time interval

我正在安排本地通知。 我的問題是,重復通知只能按每小時,每天,每周,每月或每年的間隔進行安排。 您沒有“每2小時”的選項。

現在,我希望每2或3小時的時間間隔安排一次。 此外,由於iOS的限制,一次只能安排64個通知。

請注意,我無法使用推送通知。

我怎樣才能做到這一點?

2小時= 60秒* 60分鍾* 2 = 7200秒。 這會有用嗎? 如果您在以下代碼中使用TimeInterval中的7200,則為5秒鍾( 來自Apple網站

 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)

編輯:請參閱使用UILocalNotification的類似代碼

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

如果您希望每2小時安排一次通知,則必須安排12次通知,相隔2小時,並每天重復這些通知。 這將允許您每2小時安排一次通知,無限期地重復,而不會超出您可以設置的通知數量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM