1

I've searched in all questions here but no solution worked.

I scheduled localNotifications with a userInfo option. When app is in foreground, the notification arrived and a can handle perfectly with this function that system calls by itself.

  • For local notifications

    func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
    
        if application.applicationState == .active {
            return
        }
    
        //My implementation
    }
    
  • For remote notifications

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    
        if application.applicationState == .active {
            return
        }
    
        //My implementation
    }
    

But the problem is, when app is closed, this functions are not called and I can't handle the data that I need.

I've tried get userInfo in that function in appDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if let localUserInfo = launchOptions?[UIApplicationLaunchOptionsKey.localNotification] as? [AnyHashable: Any] {

        // My implementation
    } else if let remoteUserInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {

        // My implementation
    }
    return true
}

But.. Didn't work...

Someone could help me please?

Thanks in advance.

Rafaela Lourenço
  • 1,126
  • 16
  • 33

2 Answers2

2

Have you registered for notifications? In didFinishLaunchingWithOptions():

    // Register Notifications
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { granted, error in

                if granted {
                    print("User notifications are allowed")
                } else {
                    print("User notifications are NOT allowed")
                }
            })
            application.registerForRemoteNotification()
            UNUserNotificationCenter.current().delegate = self

Then you should catch local notifications in UNUserNotificationCenterDelegate method:

extension AppDelegate: UNUserNotificationCenterDelegate {

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

        completionHandler()
    }

}
Anton Novoselov
  • 769
  • 2
  • 7
  • 19
  • I'm not using UNNotificationCenter. To get permission I'm using application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)) – Rafaela Lourenço Mar 27 '18 at 20:20
  • registerUserNotificationSettings(UIUserNotificationSettings is deprecated since iOS10. User UNUserNotificationCenter instead. Try my code – Anton Novoselov Mar 27 '18 at 20:23
  • The problem is that I configure my notifications using UILocalNotifications (I need to have support to ios 9.0 and later). And UNUserNotificationCenter is just for ios 10.0 and later.... – Rafaela Lourenço Mar 27 '18 at 20:23
  • I used UNUserNotificationCenter.current().delegate = self and worked, thanks! – Rafaela Lourenço Apr 20 '18 at 16:57
0

You will get the notification in didFinishLaunchingWithOptions if the user clicks it whether it's local or remote , otherwise you won't know except if store them in server and pull them every time you open the app

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87