alexander
alexander

Reputation: 149

remote push Notification when app is in background swift 3

I have an app that receives remote push notification. I have implemented didReceiveRemoteNotification in this way:

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


        print("PUSH NOTIFICATION is coming")

        let state: UIApplicationState = UIApplication.shared.applicationState
        let inBackground = state == .background
        let dizionario = userInfo["aps"]! as! NSDictionary
        let alert = dizionario["alert"]! as! String
        print(alert)


        if(!inBackground){

                print("APP IN FOREGROUND")
                //show alert view and the if user tap 'ok' show webview

             }
            else{

                print("APP IS BACKGROUND")
                //SHOW WEBVIEW

              }
           }

In both cases(when app is in foreground and background) I have to show webview that add like child to root view(tabbar controller) but if app is in foreground then I have to show , before , an alert view. My problem is that if app is in foreground I haven't problems , but if app is in background didReceiveRemoteNotification doesn't call(I don't see the print "PUSH NOTIFICATION is coming" ) and I don't understand why.

Can you help me?

N.B for testing I use APN Tester(https://itunes.apple.com/it/app/apn-tester-free/id626590577?mt=12) for send push notification

Upvotes: 3

Views: 10615

Answers (2)

Emircan UZEL
Emircan UZEL

Reputation: 74

In AppDelegate.swift :

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // make your function call
 }

didReceive mean is: when your Application is Background then you click the notification didReceive is working

Upvotes: 0

Mundi
Mundi

Reputation: 80265

didReceiveRemoteNotification is meant to be used when the app is active.

When the app is in the background or inactive, you can activate it by pressing the action button on the remote notification. Implement userNotificationCenter(_:didReceive:withCompletionHandler:) in your app delegate.

Upvotes: 2

Related Questions