remote push Notification when app is in background swift 3

10,042

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.

Share:
10,042
alexander
Author by

alexander

Updated on June 04, 2022

Comments

  • alexander
    alexander almost 2 years

    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

    • Paulw11
      Paulw11 over 7 years
      Have you specified "Remote notifications" in your app's background mode capabilities? Also, when your app is in the background you cannot display a web view or otherwise force your app to the foreground.
    • Gruntcakes
      Gruntcakes over 7 years
      There are two types of push, one which is delivered to the user always, and one which is delivered to the app without the user seeing it. If your app is in the background you can use this second type of push. However, as Paulw11 has pointed out, given your explanation of why you want to use it, it is pointless as there is no way you can force a background app to come into the foreground i.e. a background app cannot display any GUI (with the exception of posting a local notification).
  • Paulw11
    Paulw11 over 7 years
    That is the new iOS 10 method, but the method shown by the OP, while deprecated on iOS 10 is required for earlier versions of iOS and will still work on iOS 10.