OneSignal
Let people choose to receive push notifications sent from OneSignal. Connect it and write the code now; the SDK runs only in a native build.
- Asks for
- OneSignal App ID
- Runs in
- Native development and release builds
On this page
What the connector does
OneSignal lets people choose to receive push notifications from your app. The connector reads this device's notification permission and subscription, shows the permission prompt, and opts the device in or out.
You send notifications from the OneSignal dashboard or your own trusted server: the connector cannot send them. User linking, login, logout and aliases are not supported either.
What you need
- OneSignal App ID
- Required. The App ID with dashes, from your app in OneSignal under Settings, Keys & IDs, in the form
12345678-1234-1234-1234-123456789abc. App API keys and REST API secrets are refused: the field accepts only an App ID.
The Apple (APNs) and Firebase credentials that delivery needs are set up in OneSignal, not in Appsanic. Only one OneSignal connection can be enabled in a project at a time.
Add OneSignal
- In OneSignal, copy your app's App ID, and follow OneSignal's setup for Apple and Android push in its dashboard.
- Choose Connect in the rail, then Connect OneSignal, and enter the OneSignal App ID under Test details.
- Choose Check connection, then Connect.
The check confirms that the App ID has the right form; after Connect, the status reads Saved · further testing needed. It does not contact OneSignal or test a device: that can only happen in a native build. The steps every connector shares are in Connectors.
What you can request
| Operation | What it does | Parameters | Returns |
|---|---|---|---|
status | Starts the SDK and reads this device's permission, opt-in flag and subscription ID. | None | permission, optedIn, subscriptionId, deliveryVerified |
requestPermission | Shows the system permission prompt. With fallbackToSettings set to true, it can offer the device settings when permission was denied before. | fallbackToSettings (true or false) | granted |
optIn | Opts this device in to push notifications. The SDK may ask for permission. | None | status |
optOut | Opts this device out without changing the system permission. | None | status |
The SDK starts on the first request, so call status when the app starts. Only requestPermission and optIn ask for permission: call them from something the person taps. deliveryVerified is always false, because an opt-in flag or a subscription ID does not prove that a notification arrives.
Use it in code
Import getConnector from @appsanic/connectors with your connection's id, shown under How to use OneSignal in the connection's settings. Check availability?.supported before every operation, and show availability.reason when it is false. Where the SDK cannot run, a request throws that reason rather than pretending to work.
import { getConnector } from "@appsanic/connectors";
// Use the id of your own connection, shown under How to use OneSignal.
const push = getConnector("onesignal-4a8f02bd");
// Call once when the app starts. Returns null where the SDK cannot run.
export async function readNotificationStatus() {
if (!push.availability?.supported) return null;
return push.request("status");
}
// Call only from a switch or button that the person taps.
export async function turnOnNotifications() {
if (!push.availability?.supported) {
throw new Error(push.availability?.reason ?? "A native development build is required.");
}
await push.request("optIn");
return push.request("status");
}Each reply is an unknown value: check that permission and optedIn are both true before showing notifications as on. On a paid plan, Build with Sanic AI in the connection's settings starts a Sanic AI draft that names this connection, so you can ask for notification preferences. Sanic AI cannot set up your OneSignal account. See With Sanic AI.
Where it runs
| Where | Mode | Can the SDK run? |
|---|---|---|
| Browser preview | browser | No. Requests throw availability.reason. |
| Expo Go | expo-go | No. Requests throw availability.reason. |
| Native development or release build | native | Yes, when the build was made with this connection enabled. |
availability.mode tells your code which case it is in. A native build made without this connection reports that the native SDK is unavailable and asks for a new development build with the connector enabled. While the connection is enabled, Problems in Code also shows an Android/iOS warning that OneSignal needs a native build. The rest of your app still runs in the preview and in Expo Go: see What a preview can and cannot prove.
Limits and good practice
- Test does not prevent real notifications. Use a separate OneSignal app for test devices under Test details.
- Changing the App ID needs a restart of the native app, because the SDK holds one subscription for the whole app.
- Check delivery, a denied permission and opting out on a phone before you send to customers.
