swift - 在特定时间重复本地通知,并在同一时间间隔后重复
问题描述
它可能与问题重复 -每天在设定的时间使用 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")
}
}
我怎样才能同时实现这两件事?
解决方案
您可以使用 UNUserNotificationCenter 进行本地通知,同时,对于重复通知,您可以使用 performFetchWithCompletionHandler 方法,对于此方法,您必须设置调用方法的最小时间间隔。
还示例代码 -
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))")
}
}
}
推荐阅读
- excel - RemoveDuplicates 检索错误 5:在“列”参数上使用变量时,过程调用或参数无效
- flutter - 如何转换,替换飞镖中的字符串
- qt - 如何在 QML、Qt 中捕获没有鼠标区域的鼠标滚轮移动?
- c# - WPF ComboBox 忽略绑定
- mule - 如何在 dataweave 中使用 Excel 作为查找
- laravel - 我如何才能像 Laravel 中的 dd() 函数一样将变量数据作为字符串获取?
- authentication - Keycloak 是否支持某种形式的中间人身份验证?
- java - CloudSDK 能否帮助使用仅在运行时才知道的 OData 实体类型?
- javascript - ChartJS 根据日期更改显示的数据?
- visual-studio - 如何使用新项目格式将文件夹的文件标记为带有通配符的嵌入式资源?