Swift program to send Push Notifications

Is there a step by step program on Sending Push notifications?

I seem to be stuck at load private key. I get this error. SecKeyCreateWithData failed with error: Error Domain=NSOSStatusErrorDomain Code=-50 "EC private key creation from data failed"

It is a new p8 file. I tried different format. I read some articles that say that there is a bug I think. I don't know for sure because it was written in jibberish.

90% of the code is dealing with these stupid keys. This should be 1 function setting the pipe and then I can use the pipe. This is ridiculous. If anybody has any ideas. The code is a mess because I tried so many different ideas.

I can't say what the problem is with your key handling code without digging deep into what you admittedly call "a mess", but I can tell you that you won't be able to send any push notifications with that code either way.

You seem to be using the URL https://gateway.push.apple.com to send notifications. This is an endpoint to the old push notifications protocol which is no longer supported. APNs has since switched to HTTP/2 at https://api.push.apple.com:443 using a different request format than before. So you would want to check your sources for push notifications sending for accuracy first.

You can read more about the new APNs Provider API here: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns/

As for your key handling code, keep in mind you are doing all this to acquire a provider token (JWT) to send the notification request with. https://jwt.io/libraries will have code in many languages (including Swift) that will create these tokens. You may want to start there.


Argun Tekant /  DTS Engineer / Core Technologies

Written by Engineer in 823072022
[has] code in many languages (including Swift) that will create these tokens

Right. But focusing on the key import side of this, it is possible to do that in a few lines of code, but it’s easy to get confused by all both the APIs and the terminology. Here’s a snippet that imports a PEM-encoded PKCS#8 P256 private key, as used by Apple services like App Store Connect API, APNs, and so on:

import Foundation
import CryptoKit

let u = URL(fileURLWithPath: "/Users/quinn/test.p8")
let s = try String(contentsOf: u, encoding: .utf8)
let k = try P256.Signing.PrivateKey(pemRepresentation: s)

let d = k.x963Representation
let k2 = try secCall { SecKeyCreateWithData(d as NSData, [
    kSecAttrKeyType: kSecAttrKeyTypeECSECPrimeRandom,
    kSecAttrKeyClass: kSecAttrKeyClassPrivate,
] as NSDictionary, $0) }
print(k2)

If your JWT library uses Apple CryptoKit, you only need the first part. If your JWT library takes a SecKey, you also need the second bit.

For more information about what I’m doing here, see:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Swift program to send Push Notifications
 
 
Q