131 lines
4.1 KiB
Swift
131 lines
4.1 KiB
Swift
//
|
|
// EQNOfficialPushNotification.swift
|
|
// Earthquake Network
|
|
//
|
|
// Created by Andrea Busi on 29/06/24.
|
|
// Copyright © 2024 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import Shogun
|
|
|
|
|
|
@objc
|
|
class EQNOfficialPushNotification: NSObject, Codable {
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case latitude
|
|
case longitude
|
|
case magnitude
|
|
case date
|
|
}
|
|
|
|
private let latitude: Double
|
|
private let longitude: Double
|
|
let magnitude: Double
|
|
let date: Date?
|
|
|
|
var coordinate: CLLocation {
|
|
.init(latitude: latitude, longitude: longitude)
|
|
}
|
|
|
|
// MARK: - Init
|
|
|
|
init(
|
|
latitude: Double,
|
|
longitude: Double,
|
|
magnitude: Double,
|
|
date: Date?
|
|
) {
|
|
self.latitude = latitude
|
|
self.longitude = longitude
|
|
self.magnitude = magnitude
|
|
self.date = date
|
|
}
|
|
|
|
// MARK: - Class
|
|
|
|
/// Remove any saved notification
|
|
static func removeStored() {
|
|
UserDefaults.standard.removeObject(forKey: UserDefaults.OfficialAlertPayload)
|
|
}
|
|
|
|
/// Retrieve stored notification (if any)
|
|
static func stored() -> EQNOfficialPushNotification? {
|
|
guard let data = UserDefaults.standard.object(forKey: UserDefaults.OfficialAlertPayload) as? Data else {
|
|
print("[EQNOfficialPushNotification] No notification saved for key '\(UserDefaults.OfficialAlertPayload)'")
|
|
return nil
|
|
}
|
|
|
|
guard let notification = try? JSONDecoder().decode(EQNOfficialPushNotification.self, from: data) else {
|
|
print("[EQNOfficialPushNotification] Unable to decode given notification")
|
|
return nil
|
|
}
|
|
|
|
return notification
|
|
}
|
|
|
|
/// Convert and store a push notification payload.
|
|
/// Expected payload has the following structure:
|
|
/// ```
|
|
/// {
|
|
/// "title": "Segnalazione da rete sismica",
|
|
/// "body": "Sisma rilevato a 4km S Valfabbrica (PG)",
|
|
/// "userInfo": {
|
|
/// "data" : "2024-06-29 11:21:30",
|
|
/// ...
|
|
/// "aps": {
|
|
/// "alert" : {
|
|
/// "loc-key" : "Sisma rilevato a",
|
|
/// "title-loc-key" : "Segnalazione da rete sismica",
|
|
/// "title" : "Segnalazione da rete sismica",
|
|
/// "action-loc-key" : "",
|
|
/// "loc-args" : [
|
|
/// "6 km S Acate (RG) - M1.9"
|
|
/// ]
|
|
/// },
|
|
/// }
|
|
/// }
|
|
/// }
|
|
/// ```
|
|
/// - Parameter payload: Notification payload
|
|
/// - Returns: `true` if save succeed, `false` otherwise
|
|
@objc(storeNotificationWithPayload:)
|
|
@discardableResult
|
|
static func store(payload: [String: Any]) -> Bool {
|
|
guard let notification = from(payload: payload) else {
|
|
print("[EQNOfficialPushNotification] Unable to convert received notification")
|
|
return false
|
|
}
|
|
|
|
guard let data = try? JSONEncoder().encode(notification) else {
|
|
print("[EQNOfficialPushNotification] Unable to encode given notification")
|
|
return false
|
|
}
|
|
|
|
UserDefaults.standard.set(data, forKey: UserDefaults.OfficialAlertPayload)
|
|
return true
|
|
}
|
|
|
|
@objc
|
|
private static func from(payload: [String: Any]) -> EQNOfficialPushNotification? {
|
|
guard let userInfo = payload["userInfo"] as? [String: Any] else {
|
|
print("[EQNOfficialPushNotification] Missing required info to parse push notification")
|
|
return nil
|
|
}
|
|
|
|
let latitude = userInfo.double(forKey: "latitude") ?? 0
|
|
let longitude = userInfo.double(forKey: "longitude") ?? 0
|
|
let magnitude = userInfo.double(forKey: "magnitude") ?? 0
|
|
let dateString = userInfo.string(forKey: "data") ?? ""
|
|
let date = EQNUtility.getDateFrom(dateString)
|
|
|
|
return .init(
|
|
latitude: latitude,
|
|
longitude: longitude,
|
|
magnitude: magnitude,
|
|
date: date
|
|
)
|
|
}
|
|
}
|