在特定时间重复本地通知,并在同一时间后每隔一段时间重复一次本地通知
Posted
技术标签:
【中文标题】在特定时间重复本地通知,并在同一时间后每隔一段时间重复一次本地通知【英文标题】:Repeat Local Notification at a specific time and after same time at intervals 【发布时间】:2019-02-18 11:34:11 【问题描述】:它可能与问题重复 - Repeating local notification daily at a set time with swift 但 UILocalNotifications 已被 iOS 10 弃用
我正在开发闹钟应用程序,我需要两件事 1.一次本地通知 2. 间隔一段时间后重复
/// Code used to set notification
let content = UNMutableNotificationContent()
content.body = NSString.localizedUserNotificationString(forKey: titleOfNotification, arguments: nil)
content.userInfo=[]
工作正常的代码在准确的时间点击通知
/* ---> Working Fine --> how i can repeat this after 60 second if untouched
let triggerDaily = Calendar.current.dateComponents([.hour,.minute,], from: dates)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: weekdays.isEmpty == true ? false : true)
*/
/// ---> Making it Repeat After Time - How Time is passed here ?
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: true)
/// ---> Adding Request
let request = UNNotificationRequest(identifier:dateOfNotification, content: content, trigger: trigger) UNUserNotificationCenter.current().add(request)(error) in
if (error != nil)
print(error?.localizedDescription ?? "Nahi pta")
else
semaphore.signal()
print("Successfully Done")
我怎样才能同时实现这两件事?
【问题讨论】:
Repeating local notification daily at a set time with swift的可能重复 UILocalNotification 已弃用 【参考方案1】:本地通知可以使用UNUserNotificationCenter,同时重复通知可以使用performFetchWithCompletionHandler方法,该方法需要设置调用方法的最小时间间隔。
请点击链接了解更多详情 - https://developer.apple.com/documentation/uikit/core_app/managing_your_app_s_life_cycle/preparing_your_app_to_run_in_the_background/updating_your_app_with_background_app_refresh
ALSO 示例代码 -
func scheduleLocalNotification(subtitle: String, description: String, offerID: String)
// Create Notification Content
let notificationContent = UNMutableNotificationContent()
// Configure Notification Content
notificationContent.title = "New lead for " + subtitle
notificationContent.body = description
notificationContent.userInfo = ["aps":
["alert": [
"title": subtitle,
"body": description,
"content-available": "1"
],
"ofrid": offerID,
"type": "BLBGSync",
"landinguri": "abc.com",
]
]
// Create Notification Request
let triggertime = UNTimeIntervalNotificationTrigger(timeInterval: 3600, repeats: false)
let notificationRequest = UNNotificationRequest(identifier: "YOUR_IDENTIFIER", content: notificationContent, trigger: triggertime)
// Add Request to User Notification Center
UNUserNotificationCenter.current().add(notificationRequest) (error) in
if let error = error
print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
【讨论】:
UNTimeIntervalNotificationTrigger 是否会在距当前时间 60 分钟(3600 秒)后触发通知? 是的,通知将在 60 分钟后触发。请通过链接更清楚地了解 - hackingwithswift.com/example-code/system/…以上是关于在特定时间重复本地通知,并在同一时间后每隔一段时间重复一次本地通知的主要内容,如果未能解决你的问题,请参考以下文章
观察 currentPlaybackTime 并每隔一段时间显示一个覆盖