Push notifications
Send a OneSignal push notification from your backend.
Send a push notification
Send a push through OneSignal from your backend. await resolves with the delivery result — notification id, recipient count, and whether OneSignal accepted it.
Target recipients with playerIds, subscriptionIds, or includeSegments / excludeSegments. Set content with message, title, subtitle, imageUrl, data, or a saved templateId.
Server-only. Never import @thebdk/native/server/onesignal (or any @thebdk/native/server/* entry) into a browser bundle — it carries your OneSignal REST API key.
Input fields
The object you pass to sendPushNotification. Every field is optional, but each call needs credentials (inline or via env) and at least one targeting field.
message,title,subtitle— body, heading, and iOS subtitle.playerIds/subscriptionIdstarget the device push IDs stored fromdeviceInfo.playerId. Associate them with the authenticated user on your backend, remove that association on sign-out, and skip sending when no eligible IDs remain. A push-off app has no player ID.
BDK Preview uses BDK’s OneSignal account; your server credentials do not target its devices. Test production user-targeted sends in your own build. Keep notification destinations on an allowed app domain.includeSegments/excludeSegments— target or exclude named OneSignal segments.templateId— send a saved OneSignal template instead of inline content.imageUrl— a media attachment.onLoadUrl+urlParams— a launch URL.urlParamsentries are appended as query parameters and passed through indata.data— an arbitraryRecord<string, unknown>payload.iosBadgeType,iosBadgeCount— iOS badge controls.oneSignalAppId,oneSignalApiKey— inline credentials.targetChannel,endpoint,authorizationScheme,fetch— advanced overrides.
The OneSignalUrlParam type used by urlParams is { key: string; value: string | number | boolean }.
Read the result
sendPushNotification resolves with a PushNotificationResult. Check it to confirm delivery and surface failures.
notificationId: string | null— the OneSignal notification id.numberOfRecipients: number | null— how many recipients OneSignal reported.sentSuccessfully: boolean—trueonly when OneSignal returned noerrors.errorMessage: string | null— the errors OneSignal returned, ornullon success.raw: unknown— the full parsed OneSignal response.
A 200 OK can still carry errors (e.g. "All included players are not subscribed"), so always check sentSuccessfully — not just the absence of a thrown error. A non-2xx status throws a BdkError with code BDK_PROVIDER_ERROR.
Set credentials
Pass your OneSignal app id and REST API key inline as oneSignalAppId / oneSignalApiKey, or set them in the environment and omit them from the call.
For the app id it reads BDK_ONESIGNAL_APP_ID, then ONESIGNAL_APP_ID; for the key, BDK_ONESIGNAL_API_KEY, then ONESIGNAL_API_KEY. Inline values win. Missing either throws a BdkError with code BDK_VALIDATION_ERROR. The Authorization scheme is picked from your key automatically — override it with authorizationScheme ("Basic" | "Key" | "Bearer") if needed.
Prefer env vars — keep secrets out of your code and rotate keys without redeploying.
Call from a backend
Authenticate and authorize each send on your backend. Choose recipients and message content from server-owned state. Do not expose a route that forwards arbitrary browser recipient IDs and message text to OneSignal. Rate-limit requests and apply your app’s CSRF protection when using cookie sessions.
This example uses application-owned adapters from notification-policy: authenticated req.user, CSRF and rate limits, and an atomic claim that checks event ownership, consent, eligible recipients, message content and an allowlisted destination. Implement those checks and retry/deduplication rules in your app before exposing the route. A caller-supplied event ID is only a lookup key, never authorization.
If no fetch is available and you don't pass one, the request throws a BdkError with code BDK_PROVIDER_ERROR.
See it running in the live demo, or every native feature with its demo on thebdk.com.