-1

if remove notification was received when app was active - it's handling good. if app was at background orinactive - nothing happend. notifications are getting from Firebase Cloud Messaging. xcode 11.3.1

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    if application.applicationState == .active {
        Utils.handlePushNotification(userInfo: userInfo) // works good
    }

    if application.applicationState == .background {
        UserDefaults.standard.set(true, forKey: "openedFromPush") // doesn't work
    }

    if application.applicationState == .inactive {
        UserDefaults.standard.set(true, forKey: "openedFromPush") // doesn't work
    }
}
  • did you look through existing solutions https://stackoverflow.com/questions/21776618/didreceiveremotenotification-not-being-called-when-i-tap-on-app-icon-after-recei – timbre timbre Jan 27 '20 at 18:29

2 Answers2

0
  1. Make sure you ticked Remote notifications background mode in your app target's Signing & Capabilities options:

    Xcode

  2. Your method should call completionHandler(.newData) or completionHandler(.noData) when responding to a notification that arrived while the app was in the background.

    The Apple documentation states:

    If the user opens your app from the system-displayed alert, the system may call this method again when your app is about to enter the foreground so that you can update your user interface and display information pertaining to the notification. When a remote notification arrives, the system displays the notification to the user and launches the app in the background (if needed) so that it can call this method. Launching your app in the background gives you time to process the notification and download any data associated with it, minimizing the amount of time that elapses between the arrival of the notification and displaying that data to the user.

    As soon as you finish processing the notification, you must call the block in the handler parameter or your app will be terminated. Your app has up to 30 seconds of wall-clock time to process the notification and call the specified completion handler block. In practice, you should call the handler block as soon as you are done processing the notification.

Jakub
  • 603
  • 7
  • 18
0

i have solved my issue by using this:

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

}