Obtain a push token while you begin a Stay Exercise and use it to replace or finish your Stay Exercise with distant push notifications
Apple is scheduled to launch Stay Actions on the iOS 16.1 model.
With all of the hype buzzing round this new characteristic, iOS builders world wide are already making ready to undertake this characteristic to their iOS purposes.
Contemplating the essential implementations have been mentioned sufficient, this text goes to particularly allow you to replace your dwell actions utilizing the push notification.
We’re engaged on an app that gives a dwell replace from a race between the Avengers. The content material state for our dwell exercise seems to be like this:
struct ContentState: Codable, Hashable {
var playerOnFirst: String
var currentLap: Int
}
In response to Apple’s documentation right here, updating our dwell exercise utilizing push notifications would require our .aps payload to seem like this:
So allow us to strive updating our dwell exercise.
- First, what we want is for our app to be enabled to obtain push notifications. You are able to do this by going to your Xcode undertaking, choose your goal, and go underneath the Signing & Capabilities, and add a brand new functionality by deciding on Push Notifications. Then, go to your Apple developer web site and ensure you have a push notification key registered on your app. In the event you haven’t, go forward and create one utilizing your app’s bundle ID. Obtain your key file in a spot you’ll keep in mind and replica the Key ID and paste it someplace as a result of we’ll want it within the subsequent steps.
- Put together your dwell exercise’s push token by producing it from our exercise’s request.
- Now let’s ship our push notification through command line (For a extra detailed documentation from Apple, refer right here). For dwell actions, our apn push notifications might be utilizing a Token-based authentication. Which means that we’ll want an authorization header handed in utilizing a token. To attain that, additionally put together these:
- Group ID: That is the ID situated on the highest proper nook subsequent to your title while you open the developer website.
- Token Key file path: That is the trail to retrieve the downloaded key file from the earlier step. instance:
/Customers/stefan.hermanus/downloads/{fileName.p8}
- Auth Key ID: That is the ten digit key identifier you acquired when making a key on the earlier steps
- Now allow us to set these shell variables earlier than lastly having the ability to ship our push notification POST request. Run these strains one after the other in your terminal. Exchange the values contained in the curly brackets with those you might have ready beforehand.
% export TEAM_ID={your TEAM ID}
% export TOKEN_KEY_FILE_NAME={Token Key file path}
% export AUTH_KEY_ID={your Auth Key ID}
% export DEVICE_TOKEN={myToken from the exercise push token}
% export APNS_HOST_NAME=api.sandbox.push.apple.com
- Subsequent, to generate your authentication token, set these shell variables as properly. If in case you have set the variables on the earlier step accurately, you’ll be able to copy and paste these subsequent ones one after the other straight in your terminal. (apart from the % signal)
% export JWT_ISSUE_TIME=$(date +%s)% export JWT_HEADER=$(printf '{ "alg": "ES256", "child": "%s" }' "${AUTH_KEY_ID}" | openssl base64 -e -A | tr -- '+/' '-_' | tr -d =)% export JWT_CLAIMS=$(printf '{ "iss": "%s", "iat": %d }' "${TEAM_ID}" "${JWT_ISSUE_TIME}" | openssl base64 -e -A | tr -- '+/' '-_' | tr -d =)% export JWT_HEADER_CLAIMS="${JWT_HEADER}.${JWT_CLAIMS}"% export JWT_SIGNED_HEADER_CLAIMS=$(printf "${JWT_HEADER_CLAIMS}" | openssl dgst -binary -sha256 -sign "${TOKEN_KEY_FILE_NAME}" | openssl base64 -e -A | tr -- '+/' '-_' | tr -d =)% export AUTHENTICATION_TOKEN="${JWT_HEADER}.${JWT_CLAIMS}.${JWT_SIGNED_HEADER_CLAIMS}"
Now you might have all your variables saved, most significantly your authentication token. Allow us to generate the curl, it should look a bit of like this:
Exchange {Your App Bundle ID}
utilizing your personal app’s bundle Id. And in case you are working by yourself app, alter the aps payload to fit your personal wants.
For the timestamp in your aps
, ensure you have an up to date epoch timestamp. To generate an up to date timestamp, go right here.
Now that we have already got our curl, go forward and hit this curl in your terminal after ensuring your dwell exercise is already up and operating. That’s it!
You will notice your dwell exercise updating in the event you efficiently hit the POST request by way of the curl.