1 year ago

#373554

test-img

Eric

How to correctly formulate POST request to send simple APNs notification

I am trying to send a simple push notification with APNs. Going through the Apple Docs, I'm still a bit unsure if I am formulated my POST request correctly. Can someone verify if it's correct or if something needs to be fixed?

Note1: I am using certificate-based authentication (or at least I think I am).

Note2: apnsToken is the device token returned from application(didRegisterForRemoteNotificationsWithDeviceToken: ) (converted to a String based on @ytrewq's comment).

func sendNotification(apnsToken: String, title: String, body: String) {

    let params: Parameters = [
        "aps" : [
            "alert" : [
                "title" : title,
                "body" : body
            ]
        ]
    ]
    
    let headers: HTTPHeaders = [
        ":path": "/3/device/\(apnsToken)",
        "apns-push-type": "alert"
    ]
    
    AF.request("https://api.push.apple.com", method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).response { response in
        debugPrint(response)
    }
}

Running the code above gives me the error failure(Alamofire.AFError.sessionTaskFailed(error: Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response".

ios

swift

apple-push-notifications

0 Answers

Your Answer

Accepted video resources