-1

For my statistics dashboard inside the backend i need to call a rest-api via alamofire that informs the backend that the user opened the app through a push notification.

How can I achieve that?

Jochen Österreicher
  • 683
  • 2
  • 11
  • 25
  • did you try with **applicationWillEnterForeground** or **applicationDidBecomeActive** ? – Nazmul Hasan Jan 26 '17 at 18:16
  • but how can i then differentiate if the app was opened manually or through a push notification click? I need to see it in my dashboard if the push is effective. – Jochen Österreicher Jan 26 '17 at 18:16
  • " through a push notification click " what does it mean ? please – Nazmul Hasan Jan 26 '17 at 18:21
  • did you try with UNUserNotificationCenterDelegate - userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) – Lucas Palaian Jan 26 '17 at 18:21
  • @LucasPalaian where can I place this code? I guess the viewcontrollers can differ regarding the state of the app (cold start, or in background) – Jochen Österreicher Jan 26 '17 at 18:24
  • You can set your appdelegate to confrom that protocol. import UserNotifications, then AppDelegate: class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate and in class body userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) – Lucas Palaian Jan 26 '17 at 18:26
  • I used it with local notifications, i dont know how works push notifications but i think its the same thing when talking about user interaction – Lucas Palaian Jan 26 '17 at 18:27
  • @LucasPalaian ok i try it at home, in the meantime you can write a full answer for me to mark as correct – Jochen Österreicher Jan 26 '17 at 18:32

1 Answers1

1

I worked with local notifications so i will give you an example about what i do. I dont know if its the same way for push notifications.

What i do is to set appdelegate to conform to User Notifications delegate

1: Import

import UserNotifications

2: Add protocol

 class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate

3: Notification Center Instance

var notificationCenter: UNUserNotificationCenter!

4: Initialize and set delegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    notificationCenter = UNUserNotificationCenter.current()
    notificationCenter.delegate = self

    return true  
}

5: NotificationCenter response

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

       print("notification pressed") 
}
Lucas Palaian
  • 374
  • 2
  • 12