Commit 541f3a3c by Demid Merzlyakov

MoEngage push notifications handling.

parent 7d111caa
......@@ -18,4 +18,5 @@ public enum AnalyticsParameter: String {
case ANALYTICS_KEY_PLACEMENT_NAME = "placement_name"
case ANALYTICS_KEY_AD_UNIT_ID = "AD_PLACEMENT_ID"
case ANALYTICS_KEY_AD_ADAPTER = "AD_ADAPTER"
case ANALYTICS_KEY_PUSH_NOTIFICATION_SOURCE = "source"
}
......@@ -70,11 +70,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
MoEngage.sharedInstance().initializeLive(with: moEngageConfig, andLaunchOptions: launchOptions)
#endif
PushNotificationsManager.shared.registerForRemoteNotifications()
return true
}
func applicationDidBecomeActive(_ application: UIApplication) {
LocationManager.shared.updateAllWeatherIfNeeded()
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
PushNotificationsManager.shared.set(pushToken: deviceToken)
}
}
......@@ -55,7 +55,7 @@ class DeeplinksRouter {
return false
}
private func parseLocation(from url: URL) -> PartialLocation? {
public func parseLocation(from url: URL) -> PartialLocation? {
guard let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems else {
return nil
}
......
......@@ -421,16 +421,6 @@ public class LocationManager {
}
private func makeLocation(from partialLocation: PartialLocation, completion: @escaping (Location?) -> ()) {
guard let latStr = partialLocation.lat, let lonStr = partialLocation.lon, let lat = CLLocationDegrees(latStr), let lon = CLLocationDegrees(lonStr) else {
log.error("Geo lookup: no coordinates present: \(partialLocation)")
var location: Location? = nil
if partialLocation.deviceLocation {
log.debug("Adding a placeholder empty device location.")
location = Location(deviceLocation: true, timeZone: TimeZone.current)
}
completion(location)
return
}
if let fipsCode = partialLocation.fipsCode {
if let existingLocation = locations.first(where: { $0.fipsCode == fipsCode }) {
completion(existingLocation)
......@@ -443,6 +433,16 @@ public class LocationManager {
return
}
}
guard let latStr = partialLocation.lat, let lonStr = partialLocation.lon, let lat = CLLocationDegrees(latStr), let lon = CLLocationDegrees(lonStr) else {
log.error("Geo lookup: no coordinates present: \(partialLocation)")
var location: Location? = nil
if partialLocation.deviceLocation {
log.debug("Adding a placeholder empty device location.")
location = Location(deviceLocation: true, timeZone: TimeZone.current)
}
completion(location)
return
}
let location = CLLocation(latitude: lat, longitude: lon)
......
......@@ -8,7 +8,7 @@
import Foundation
import MoEngage
/*
class PushNotificationsManager: NSObject {
// MARK: - Private
......@@ -66,26 +66,28 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
return nil
}
private func switchLocationIfNeeded(parsing screenData: [String: Any]?, using router: Router) {
private func switchLocationIfNeeded(parsing screenData: [String: Any]?, using router: DeeplinksRouter) {
guard let screenData = screenData else {
return
}
if let cityId = screenData["location"] as? String {
let lat = screenData["lat"] as? String
let lon = screenData["lon"] as? String
if let location = WeatherUpdateManager.shared.locationFrom(cityId: cityId, lat: lat, lon: lon) {
log.info("MoEngage push: location found: \(location)")
WeatherUpdateManager.shared.addLocation(location)
}
else {
log.error("MoEngage push:Location found but couldn't be parsed.")
}
}
else {
log.debug("MoEngage push: no cityId found")
let cityId = screenData["location"] as? String
let lat = screenData["lat"] as? String
let lon = screenData["lon"] as? String
let fipsCode = screenData["fipsCode"] as? String
guard (lat != nil && lon != nil) || cityId != nil || fipsCode != nil else {
log.debug("MoEngage push: no location data found")
return
}
let newLoc = GeoNamesPlace()
newLoc.latitude = lat
newLoc.longitude = lon
newLoc.optionalCityId = cityId
newLoc.fipsCode = fipsCode
LocationManager.shared.addIfNeeded(partialLocation: newLoc, selectLocation: true)
log.info("MoEngage push: location found: \(newLoc)")
}
private func handleMoEngageDeeplinks(for response: UNNotificationResponse) {
......@@ -98,12 +100,7 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
log.info("MoEngage push received: \(userInfo)")
guard let tabBar = TabBarController.viewController else {
log.error("No tab bar found to initialize Router")
return
}
let router = Router(tabBar: tabBar)
let router = DeeplinksRouter()
if let moEngageUrl = makeMoEngageDeeplinkUrl(from: response) {
router.open(url: moEngageUrl)
}
......@@ -135,7 +132,7 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
case "0":
router.openToday()
case "1":
router.openForecast(detailType: nil)
router.openForecast(timePeriod: nil)
case "2":
router.openPrecipitation()
case "3":
......@@ -151,7 +148,6 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
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
guard !pushPinManager.processPushPinNotification(response) else { return }
handleMoEngageDeeplinks(for: response)
completionHandler()
}
......@@ -159,13 +155,13 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if UIApplication.shared.applicationState == .active {
analyticsLogEvent(ANALYTICS_PUSH_RECEIVED)
analytics(log: .ANALYTICS_PUSH_RECEIVED)
} else {
analyticsLogEvent(ANALYTICS_PUSH_SELECTED, params: ["source": "background"])
analytics(log: .ANALYTICS_PUSH_SELECTED, params: [.ANALYTICS_KEY_PUSH_NOTIFICATION_SOURCE: "background"])
}
log.debug("Got a push notification: \(notification.request.content.userInfo)");
completionHandler([.sound,.alert])
}
}
*/
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