Ich möchte die automatische Push-Benachrichtigung in meiner App verwenden. Ich habe Hintergrundmodi in meiner App und meinem Telefon aktiviert.HTTP-Anforderung mit stiller Benachrichtigung ausführen
Wenn Stille Benachrichtigung ankommt didReceiveRemoteNotification
Delegate-Methode ruft auf. Aber ich kann keine HTTP-Anfrage in dieser Methode machen. Ich verwende folgenden Code:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("step 1")
let request = NSMutableURLRequest(URL: NSURL(string: "url")!)
request.HTTPMethod = "POST"
let postString = "search=\(username)&me=\(anonId)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
print("step 2")
if error == nil {
let jsonResult: Dictionary = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! Dictionary<String, AnyObject>
let results = jsonResult["result"] as! [[String: AnyObject]]
print(results)
for item in results {
}
completionHandler(UIBackgroundFetchResult.NewData)
}
}
}
Wenn Meldung kommt, sehe ich step 1
Text auf der Konsole. Aber ich kann step 2
Text nicht sehen.
Wie kann ich dieses Problem beheben?
Können Sie ein kleines Beispiel geben? –