live-activity-apn
v0.0.1
Published
A TypeScript APNs client for Live Activities and alert notifications.
Maintainers
Readme
live-activity-apn
Send Apple Push Notification service (APNs) requests for:
- Live Activity start/update/end events
- Standard APNs alert pushes
This package is a lightweight TypeScript wrapper around APNs HTTP/2 calls.
Installation
npm install live-activity-apnRequirements
- Apple Developer account with APNs enabled
- APNs auth key (
.p8) with:team_idkey_id- local
key_pathto the key file
- App
bundle_id - Device token(s) generated by your iOS app
About the .p8 key (APNs Auth Key)
You generate this from the Apple Developer portal:
- Go to Apple Developer Certificates, Identifiers & Profiles.
- Create a new key and enable Apple Push Notifications service (APNs).
- Download the key file (
AuthKey_XXXXXXXXXX.p8) immediately (Apple lets you download it only once). - Save it securely on your server and set:
key_path-> absolute path to this.p8filekey_id-> the key identifier shown in the portalteam_id-> your Apple Developer Team ID
Obtain Live Activity Tokens (iOS)
Use the following ActivityKit snippets in your iOS app to collect the required tokens.
Important token usage:
- The token from
pushToStartTokenUpdatescan only be used to start a live activity. - The token from
activity.pushTokenUpdates(insideactivityUpdates) should be used to update or end that live activity.
Push-to-start token (iOS 17.2+)
if #available(iOS 17.2, *) {
Task {
for await tokenData in Activity<NotificationAttributes>.pushToStartTokenUpdates {
let token = tokenData.map { String(format: "%02x", $0) }.joined()
print("pushToStart token: \(token)")
// you can later read this from your plugin and send to server
}
}
}Per-activity update token (iOS 16.1+)
if #available(iOS 16.1, *) {
Task {
for await activity in Activity<NotificationAttributes>.activityUpdates {
for await tokenData in activity.pushTokenUpdates {
let token = tokenData.map { String(format: "%02x", $0) }.joined()
print("Update token for activity \(activity.id): \(token)")
}
}
}
}Obtain Device Token for Normal Notifications
Use this Swift callback to convert the APNs deviceToken into a hex string for standard alert pushes.
Swift
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
print("APNs device token: \(token)")
}Basic Usage
import LiveActivityClient from "live-activity-apn";
const client = new LiveActivityClient({
app_id: "com.example.app",
bundle_id: "com.example.app",
key_id: "ABCD1234",
key_path: "/absolute/path/AuthKey_ABCD1234.p8",
team_id: "TEAM123456"
});
await client.startLiveActivity("DEVICE_TOKEN", {
content_state: {
title: "Order placed",
body: "Preparing your order"
},
attributes_type: "NotificationAttributes",
attributes: {
id: "order-123"
}
});API
new LiveActivityClient(config)
config fields:
team_id: Apple Team IDkey_id: APNs key IDkey_path: filesystem path to.p8keybundle_id: your app bundle identifierapp_id: app identifier value kept for consumer metadata/use
startLiveActivity(deviceToken, payloadState)
Sends APNs event: "start" payload.
updateLiveActivity(deviceToken, payloadState)
Sends APNs event: "update" payload.
endLiveActivity(deviceToken, payloadState?)
Sends APNs event: "end" payload.
alert(deviceToken, alert)
Sends a standard APNs alert push (apns-push-type: alert).
Payload Notes
payloadState.content_stateshould match your ActivityKit model.attributes_typeandattributesare typically required forstartevents.relevance_scoreis optional and used in update payloads.
Environment and APNs Host
The APNs host is selected via NODE_ENV:
production->https://api.push.apple.com- any other value ->
https://api.sandbox.push.apple.com
Running Local Examples
- Copy
.env.exampleto.env - Fill all required values
- Run one of:
npm run example:start
npm run example:update
npm run example:end
npm run example:alertBuild
npm run buildThe package entrypoint exports from dist/index.js with type definitions.
Contributing
Contributions are welcome. Feel free to open issues for bugs/feature requests and submit pull requests for improvements.
