'Swift Push Notifications send if api change
How to send notification when api changes its data? I am getting data from url. When the data at this address changes, the user must receive it. I tried to use "UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)" but it sends the notification without change.
func apiNotification(urlString:String){
if let url = URL(string: urlString) {
if let data = try? Data(contentsOf: url) {
parse(jsonData: data)
}
}
}
private func notificationSend(id:String, title: String, introtext: String){
let content = UNMutableNotificationContent()
content.subtitle = title
content.body = introtext
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 30, repeats: false)
let uid = UUID.init().uuidString
let request = UNNotificationRequest(identifier: uid, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if let error = error {
print("Error \(error.localizedDescription)")
} else{
print("Notification Register Success")
print("===================================")
}
}
}
private func parse(jsonData: Data) {
do {
let decodedData = try JSONDecoder().decode(ApiData.self, from: jsonData)
notificationSend(id:decodedData.id, title: decodedData.title, introtext: decodedData.introtext)
} catch {
print("decode error")
}
}
API JSON:
{
"id": "176",
"title": "Notification title 1",
"introtext": "Notification introtext 1",
}
Changed JSON API:
{
"id": "177",
"title": "Notification title 2",
"introtext": "Notification introtext 2",
}
Please help me
Solution 1:[1]
Your API must send an event when the data is changing. You must use the framework that Apple is providing; you need to enable this in the app settings, under capabilities, then push notifications. And your API needs to use a key registered in the Apple Developer Platform.
More details in this tutorial: https://www.raywenderlich.com/11395893-push-notifications-tutorial-getting-started
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jeremy Caney |
