Commit cf8f3803 by Demid Merzlyakov

Implemented subscription to NWS notifications.

parent 6358be94
......@@ -138,6 +138,13 @@ class DeeplinksRouter {
}
}
public func openAlerts() {
DispatchQueue.main.async {
self.log.info("open Alerts")
self.appCoordinator.openNotifications()
}
}
public func openPrecipitation() {
DispatchQueue.main.async {
self.log.info("open Precipitation")
......
......@@ -23,6 +23,7 @@ public class LocationManager {
private let healthSource: HealthSource
private let fipsSource: FIPSSource
public let nwsAlertsManager: NWSAlertsManager
private let pushNotificationsManager: PushNotificationsManager
private let storage: Storage
private var defaultLocation = Location(deviceLocation: false,
coordinates: .init(latitude: 37.3230, longitude: -122.0322), // Cupertino
......@@ -156,16 +157,18 @@ public class LocationManager {
healthSource: BlendHealthSource(),
nwsAlertsManager: NWSAlertsManager(),
fipsSource: BlendFIPSSource(),
pushNotificationsManager: PushNotificationsManager.shared,
storage: DelayedSaveStorage(storage: CoreDataStorage(), delay: 2))
public let maxLocationsCount = 12
public init(weatherUpdateSource: WeatherSource, healthSource: HealthSource, nwsAlertsManager: NWSAlertsManager, fipsSource: FIPSSource, storage: Storage) {
public init(weatherUpdateSource: WeatherSource, healthSource: HealthSource, nwsAlertsManager: NWSAlertsManager, fipsSource: FIPSSource, pushNotificationsManager: PushNotificationsManager, storage: Storage) {
self.weatherUpdateSource = weatherUpdateSource
self.healthSource = healthSource
self.deviceLocationMonitor = DeviceLocationMonitor()
self.nwsAlertsManager = nwsAlertsManager
self.fipsSource = fipsSource
self.pushNotificationsManager = pushNotificationsManager
self.storage = storage
self.deviceLocationMonitor.delegate = self
......@@ -209,17 +212,21 @@ public class LocationManager {
updateNotifications(for: location)
getFipsIfNeeded(for: location)
}
pushNotificationsManager.updateNwsSubscriptions(for: locations)
}
public func getFipsIfNeeded(for location: Location) {
if location.fipsCode == nil {
fipsSource.getFipsCode(for: location) { [weak self] (fipsCode) in
if let fipsCode = fipsCode {
self?.makeChanges(to: location, in: "getFips") { (location) -> Location in
self?.makeChanges(to: location, in: "getFIPS", changes: { (location) -> Location in
var updatedLocation = location
updatedLocation.fipsCode = fipsCode
return updatedLocation
}
}, completion: { [weak self] in
guard let self = self else { return }
self.pushNotificationsManager.updateNwsSubscriptions(for: self.locations)
})
}
}
}
......@@ -339,8 +346,11 @@ public class LocationManager {
}
}
private func makeChanges(to location: Location, in operation: String, changes: @escaping (Location) -> Location) {
private func makeChanges(to location: Location, in operation: String, changes: @escaping (Location) -> Location, completion: (() -> ())? = nil) {
DispatchQueue.main.async {
defer {
completion?()
}
if let indexToUpdate = self.locations.firstIndex(where: { $0 == location }) {
self.locations[indexToUpdate] = changes(self.locations[indexToUpdate])
}
......
......@@ -9,7 +9,7 @@
import Foundation
import MoEngage
class PushNotificationsManager: NSObject {
public class PushNotificationsManager: NSObject {
// MARK: - Private
private let log = Logger(componentName: "PushNotificationsManager")
......@@ -41,9 +41,16 @@ class PushNotificationsManager: NSObject {
}
}
private var lastSetFIPSList = ""
public func updateNwsSubscriptions(for locations: [Location]) {
let fipsCodes = locations.compactMap { $0.fipsCode }
MoEngage.sharedInstance().setUserAttribute(fipsCodes.joined(separator: ","), forKey: "FIPS_LIST")
let newFipsList = fipsCodes.joined(separator: ",")
if newFipsList != lastSetFIPSList {
log.info("Set FIPS_LIST to '\(newFipsList)'")
lastSetFIPSList = newFipsList
}
MoEngage.sharedInstance().setUserAttribute(newFipsList, forKey: "FIPS_LIST")
}
public func set(pushToken: Data) {
......@@ -59,6 +66,7 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
case mainScreen = "com.handmark.expressweather.ui.activities.mainactivity"
case detailsScreen = "com.handmark.expressweather.ui.activities.weatherdetailsactivity"
case videosScreen = "com.handmark.expressweather.ui.activities.videodetailsactivity"
case alertsScreen = "com.handmark.expressweather.ui.activities.alertactivity"
}
private func makeMoEngageDeeplinkUrl(from response: UNNotificationResponse) -> URL? {
......@@ -147,17 +155,19 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
default:
log.error("MoEngage push: Unknown launch screen id: \(launchScreenId)")
}
case .alertsScreen:
router.openAlerts()
}
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
MoEngage.sharedInstance().userNotificationCenter(center, didReceive: response) // not sure we should call it for PushPin notifications, too
handleMoEngageDeeplinks(for: response)
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if UIApplication.shared.applicationState == .active {
analytics(log: .ANALYTICS_PUSH_RECEIVED)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment