0

When app is in background and connected to Xcode, everything works fine but when i unplug any iOS device and run the app, move to background and send remote notification, didReceiveRemoteNotification:fetchCompletionHandler not being called.

Here is the code snippet:

func application(_ application: UIApplication,
                 didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Failed to register: \(error)")
}

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

    if UserDefaults.standard.value(forKey: Constants.Key.authToken) != nil {

        print("Payload is: \(userInfo)")

        var dataDict : [String:Any] = [:]
        if (userInfo["data"] != nil){
            dataDict = userInfo["data"] as! [String : Any]
        }

        // ID : notification type
        var idType = 0
        if (dataDict["id"] != nil){
            idType = dataDict["id"] as! Int
        }

        let state = UIApplication.shared.applicationState
        print("state is \(state)")

        // Application in forground state...
        if state == .active {
            self.postNotificationOfType(idType, dict: dataDict)
        }

        // Application in background state...
        else if state == .background || state == .inactive {
            self.postNotificationOfType(idType, dict: dataDict)
        }


    }
    else{
        print("User is logged out!")
    }

    completionHandler(.newData)

}

func postNotificationOfType(_ idType:Int, dict:[String:Any]){

    // service request received
    if idType == 1{

        if (dict["serviceRequestId"] != nil){
            NotificationCenter.default.post(name: Notification.Name("ServiceRequestReceived"), object: nil, userInfo: dict)
        }
    }

    // service request canceled by client
    else if idType == 2 {
        NotificationCenter.default.post(name: Notification.Name("ServiceRequestCanceled"), object: nil)
    }

    // service started by client
    else if idType == 5 {
        NotificationCenter.default.post(name: Notification.Name("ServiceStartedByClient"), object: nil)
    }

        // service started by client
    else if idType == 6 {
        NotificationCenter.default.post(name: Notification.Name("ServiceEndedByClient"), object: nil)
    }
}

Here is my payload:

 {
"aps" : {
    "content-available" : 1
},
"data" : 1

}

Any help will be appreciated. Thanks in advance.

Arun
  • 483
  • 6
  • 20
  • that's a known issue in iOS11.0 -> apple fixed it with iOS11.1 -> see also https://stackoverflow.com/questions/46330053/ios11-swift-silent-push-background-fetch-didreceiveremotenotification-is-not/46392357#46392357 – AlexWoe89 Nov 21 '17 at 13:32
  • Still an issue with iOS 11.1.1 and I didn't get any help from your reference link. – Arun Nov 29 '17 at 07:28

0 Answers0