可能与所问问题重复-Repeating local notification daily at a set time with swift 但是iOS 10弃用了UILocalNotifications
我正在使用警报应用程序,我需要两件事 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")
}
}
我如何同时实现这两件事?
答案 0 :(得分:0)
您可以将UNUserNotificationCenter用于本地通知,同时,对于重复通知,可以使用performFetchWithCompletionHandler方法,对于此方法,您必须设置要调用的方法的最小时间间隔。
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))")
}
}
}