1

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".

Eric
  • 569
  • 4
  • 21
  • 1
    you need to convert data to hexadecimal string, as explained here: https://stackoverflow.com/questions/47386207/getting-remote-notification-device-token-in-swift-4 – timbre timbre Apr 04 '22 at 15:58
  • Updated. Still getting the posted error though... any other ideas? – Eric Apr 04 '22 at 17:57
  • converting data to hexadecimal string is a requirement. What else is wrong with your code I don't know, and that's not an SO format. You need to distill each question to a separate SO question. – timbre timbre Apr 04 '22 at 19:08
  • I did as you stated and upvoted your comment. My code is still not working though so the question was not fully answered in my opinion. But if you are saying that that was the only error in the code, then please post your comment as an answer and I will accept it, and then I will ask a different one related to my error. Thanks! – Eric Apr 04 '22 at 19:48
  • 1
    Unless you are testing with a released version of your app (ie from the App Store) you need to connect to `api.development.push.apple.com` – Paulw11 Apr 04 '22 at 20:27
  • @Eric Comment is not the answer (and no, I am not getting points for it either). IMO you didn't provide enough info for a proper answer, so all I did is pointed to obvious error in code that should take you one step forward. I don't plan to guide you until your code is successful. Also the format "solve all problems with my code" doesn't fit SO: it's 1 question at the time. If you want a code review, you may have a better luck with https://codereview.stackexchange.com/ – timbre timbre Apr 04 '22 at 21:21
  • Thanks @Paulw11. I did not know that. I'm still getting the same error though. Is my header key (":path") incorrect? Should it just be "path" without the colon? I get a 404 error when its just "path" (which is something new I guess), and if I just put my URL as `https://api.push.apple.com/3/device/\(apnsToken)` I get a 403 (Forbidden) error... Does that mean I need to regenerate my cert or something? – Eric Apr 04 '22 at 22:26
  • Also, @ytrewq codereview.stackexchange.com is for working code only. Mine is not working so it is appropriate for this site. – Eric Apr 04 '22 at 22:31
  • `:path` is correct. A 403 indicates that you are not authenticating correctly. How are you adding your certificate to the request? I can't see that it in the code you have shown. Is this just experimenting? In general you should not send push notifications directly from an iOS app as it will expose your authentication secret. You would typically use a server. There are lots of packages for Python & Node.js that can handle sending the push for you. – Paulw11 Apr 04 '22 at 22:33
  • How about ssl request ? – Ritesh Aryal Nov 28 '22 at 10:41

0 Answers0