Commit cf8f3803 by Demid Merzlyakov

Implemented subscription to NWS notifications.

parent 6358be94
...@@ -138,6 +138,13 @@ class DeeplinksRouter { ...@@ -138,6 +138,13 @@ class DeeplinksRouter {
} }
} }
public func openAlerts() {
DispatchQueue.main.async {
self.log.info("open Alerts")
self.appCoordinator.openNotifications()
}
}
public func openPrecipitation() { public func openPrecipitation() {
DispatchQueue.main.async { DispatchQueue.main.async {
self.log.info("open Precipitation") self.log.info("open Precipitation")
......
...@@ -23,6 +23,7 @@ public class LocationManager { ...@@ -23,6 +23,7 @@ public class LocationManager {
private let healthSource: HealthSource private let healthSource: HealthSource
private let fipsSource: FIPSSource private let fipsSource: FIPSSource
public let nwsAlertsManager: NWSAlertsManager public let nwsAlertsManager: NWSAlertsManager
private let pushNotificationsManager: PushNotificationsManager
private let storage: Storage private let storage: Storage
private var defaultLocation = Location(deviceLocation: false, private var defaultLocation = Location(deviceLocation: false,
coordinates: .init(latitude: 37.3230, longitude: -122.0322), // Cupertino coordinates: .init(latitude: 37.3230, longitude: -122.0322), // Cupertino
...@@ -156,16 +157,18 @@ public class LocationManager { ...@@ -156,16 +157,18 @@ public class LocationManager {
healthSource: BlendHealthSource(), healthSource: BlendHealthSource(),
nwsAlertsManager: NWSAlertsManager(), nwsAlertsManager: NWSAlertsManager(),
fipsSource: BlendFIPSSource(), fipsSource: BlendFIPSSource(),
pushNotificationsManager: PushNotificationsManager.shared,
storage: DelayedSaveStorage(storage: CoreDataStorage(), delay: 2)) storage: DelayedSaveStorage(storage: CoreDataStorage(), delay: 2))
public let maxLocationsCount = 12 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.weatherUpdateSource = weatherUpdateSource
self.healthSource = healthSource self.healthSource = healthSource
self.deviceLocationMonitor = DeviceLocationMonitor() self.deviceLocationMonitor = DeviceLocationMonitor()
self.nwsAlertsManager = nwsAlertsManager self.nwsAlertsManager = nwsAlertsManager
self.fipsSource = fipsSource self.fipsSource = fipsSource
self.pushNotificationsManager = pushNotificationsManager
self.storage = storage self.storage = storage
self.deviceLocationMonitor.delegate = self self.deviceLocationMonitor.delegate = self
...@@ -209,17 +212,21 @@ public class LocationManager { ...@@ -209,17 +212,21 @@ public class LocationManager {
updateNotifications(for: location) updateNotifications(for: location)
getFipsIfNeeded(for: location) getFipsIfNeeded(for: location)
} }
pushNotificationsManager.updateNwsSubscriptions(for: locations)
} }
public func getFipsIfNeeded(for location: Location) { public func getFipsIfNeeded(for location: Location) {
if location.fipsCode == nil { if location.fipsCode == nil {
fipsSource.getFipsCode(for: location) { [weak self] (fipsCode) in fipsSource.getFipsCode(for: location) { [weak self] (fipsCode) in
if let fipsCode = fipsCode { 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 var updatedLocation = location
updatedLocation.fipsCode = fipsCode updatedLocation.fipsCode = fipsCode
return updatedLocation 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 { ...@@ -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 { DispatchQueue.main.async {
defer {
completion?()
}
if let indexToUpdate = self.locations.firstIndex(where: { $0 == location }) { if let indexToUpdate = self.locations.firstIndex(where: { $0 == location }) {
self.locations[indexToUpdate] = changes(self.locations[indexToUpdate]) self.locations[indexToUpdate] = changes(self.locations[indexToUpdate])
} }
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
import Foundation import Foundation
import MoEngage import MoEngage
class PushNotificationsManager: NSObject { public class PushNotificationsManager: NSObject {
// MARK: - Private // MARK: - Private
private let log = Logger(componentName: "PushNotificationsManager") private let log = Logger(componentName: "PushNotificationsManager")
...@@ -41,9 +41,16 @@ class PushNotificationsManager: NSObject { ...@@ -41,9 +41,16 @@ class PushNotificationsManager: NSObject {
} }
} }
private var lastSetFIPSList = ""
public func updateNwsSubscriptions(for locations: [Location]) { public func updateNwsSubscriptions(for locations: [Location]) {
let fipsCodes = locations.compactMap { $0.fipsCode } 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) { public func set(pushToken: Data) {
...@@ -59,6 +66,7 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate { ...@@ -59,6 +66,7 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
case mainScreen = "com.handmark.expressweather.ui.activities.mainactivity" case mainScreen = "com.handmark.expressweather.ui.activities.mainactivity"
case detailsScreen = "com.handmark.expressweather.ui.activities.weatherdetailsactivity" case detailsScreen = "com.handmark.expressweather.ui.activities.weatherdetailsactivity"
case videosScreen = "com.handmark.expressweather.ui.activities.videodetailsactivity" case videosScreen = "com.handmark.expressweather.ui.activities.videodetailsactivity"
case alertsScreen = "com.handmark.expressweather.ui.activities.alertactivity"
} }
private func makeMoEngageDeeplinkUrl(from response: UNNotificationResponse) -> URL? { private func makeMoEngageDeeplinkUrl(from response: UNNotificationResponse) -> URL? {
...@@ -147,17 +155,19 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate { ...@@ -147,17 +155,19 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
default: default:
log.error("MoEngage push: Unknown launch screen id: \(launchScreenId)") 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 MoEngage.sharedInstance().userNotificationCenter(center, didReceive: response) // not sure we should call it for PushPin notifications, too
handleMoEngageDeeplinks(for: response) handleMoEngageDeeplinks(for: response)
completionHandler() 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 { if UIApplication.shared.applicationState == .active {
analytics(log: .ANALYTICS_PUSH_RECEIVED) 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