Notifications & badges

Check authorization, ask for push permission, open settings, and set the app-icon badge.

Check notification status

Examples reuse the single browser client from Set up the client. Adjust the ~/lib/bdk import to your app’s path; do not create a client in each component or handler.

Critical alerts and time-sensitive notifications are not available today. Do not offer controls to enable them.

Read the current notification authorization. The response also includes time-sensitive and critical-alert status fields; these fields do not mean those features are available to customers.

PropertyTypeDescription
authorizationstringauthorized, denied, notDetermined, provisional, or ephemeral.
timeSensitiveobject{ supported, setting?, channelId?, channelImportance?, channelEnabled? }.
criticalobject{ supported, authorization?, canRequest? }.
platformstringios or android.

In a plain browser the call resolves ok: false with a code such as common/feature_disabled. Branch on !result.ok.

import { isBdkError } from "@thebdk/native";
import { bdk } from "~/lib/bdk";

async function runAction1() {
  const result = await bdk.notifications.status();

  if (isBdkError(result)) {
    return;
  }

  console.log(result.authorization, result.platform);
  console.log(result.timeSensitive.supported, result.critical.supported);
}
// Call runAction1() from this UI’s user action.

Ask for push permission

Read the current push authorization, then request it. granted is true for both granted and provisional. mode is the app's prompt timing: automatic asks when the app opens; manual never asks until you call request(), so show your own explainer first. Read it from status() — it can change without an app update. Call request() after await bdk.ready(), from a user tap.

PropertyTypeDescription
statusstringgranted, denied, notDetermined, or provisional.
grantedbooleanTrue for granted and provisional.
canPromptbooleanWhether request() can still ask the user.
canOpenSettingsbooleanWhether the app can open notification settings.
modestringautomatic or manual. The app's prompt timing.

This feature can be switched off in a given app build. Check await bdk.capabilities.has("push.prompt") before showing the UI. When it is off, the call resolves ok: false with code: "common/feature_disabled". See Detect features.

import { isBdkError } from "@thebdk/native";
async function runAction2() {
  if (!(await bdk.capabilities.has("push.prompt"))) {
    // Hide the prompt UI — push is off in this app.
    return;
  }

  const state = await bdk.push.status();

  if (isBdkError(state)) {
    return;
  }

  if (state.granted) {
    // Already allowed — includes provisional.
  } else if (state.canPrompt) {
    const result = await bdk.push.request();
    if (isBdkError(result)) return;
    console.log(result.status, result.granted);
  }
}
// Call runAction2() from this UI’s user action.

Open notification settings

Send the user to the OS notification settings after a denial. notifications.openSettings() opens the general settings; push.openSettings() provides the same task from the push-prompt namespace.

import { isBdkError } from "@thebdk/native";
async function runAction4() {
  const result = await bdk.notifications.openSettings();

  if (isBdkError(result)) return;
  // result.opened === true — the OS settings screen was requested.
}
// Call runAction4() from this UI’s user action.

Critical alerts and time-sensitive notifications

Critical alerts and time-sensitive notifications are not available today. The SDK exposes related types and helpers, but customers cannot enable these features.

Set the app badge

Read, set, or clear the app-icon badge. iOS sets an exact count. Android set is unsupported (ok: false, code: "common/unsupported_platform"); get is an approximate count of active notifications (source: "active_notifications", approximate: true). clear works on both — on Android it reports how many cancellable notifications were removed.

Pass a finite non-negative integer to set. Check canSet() before offering a setter.

This feature can be switched off in a given app build. Check await bdk.capabilities.has("badge") before showing the UI. When it is off, the call resolves ok: false with code: "common/feature_disabled". See Detect features.

import { isBdkError } from "@thebdk/native";
async function runAction8() {
  if (await bdk.badge.canSet()) {
    const result = await bdk.badge.set(5);
    if (isBdkError(result)) return;
  }
}
// Call runAction8() from this UI’s user action.

Associate a device with a signed-in user

After sign-in, wait for device info and send playerId to your authenticated backend. Subscribe to deviceInfo before reading the initial value so changes are not missed. Reconcile a changed or null ID; remove the user-device association on sign-out. Remove this page’s listener when its UI unmounts. Push-off apps report null. Your server selects eligible recipients; do not trust a browser’s list of other users’ device IDs.

BDK Preview uses BDK’s OneSignal account. Test your server’s targeted sends in your own app build.

React to permission changes

Refresh status after the user answers a prompt or returns from Settings. permissions.changed fires when a tracked permission changes — not on the first snapshot — so subscribe before you call request or openSettings.

PropertyTypeDescription
changedstring[]Type names that changed — includes push when notification authorization changes.
previousobjectPrior status per changed type.
permissionsobjectFull current permission map.
import { isBdkError } from "@thebdk/native";
// Register once when this UI mounts.
const offPermissions = bdk.on("permissions.changed", async ({ changed }) => {
  if (!changed.includes("push")) return;

  const push = await bdk.push.status();
  if (!isBdkError(push)) console.log(push.status, push.granted);

  const notifications = await bdk.notifications.status();
  if (!isBdkError(notifications)) console.log(notifications.authorization);
});

function cleanupPermissionsUI() {
  offPermissions();
}
// Call cleanupPermissionsUI() when this UI unmounts.

async function runAction10() {
  await bdk.push.request();
  // or: await bdk.notifications.openSettings();
}
// Call runAction10() from this UI’s user action.

Handle notification taps

Use push.onClick for live taps and retained cold-start taps. It returns an unsubscribe function; call it when this UI unmounts. Replayed taps are deduplicated by notification id and action id. Each click is a BdkPushClickRecord.

Check capabilities.has("push.click") before offering tap handling. Treat notification data as input: validate the destination and check the signed-in account’s access before opening protected content.

import { bdk } from "~/lib/bdk";

const offClick = bdk.push.onClick((click) => {
  console.log(click.id, click.actionId, click.additionalData);
  // Validate the destination and authorize access in your application.
});

// When this UI unmounts:
// offClick();

Read retained notification taps

push.getPendingClicks() drains retained clicks, newest first. Use it for manual handling instead of onClick replay. For live events alone, subscribe with bdk.on("push.clicked", listener); it does not provide the helper’s retained-click replay.

import { isBdkError } from "@thebdk/native";

try {
  const result = await bdk.push.getPendingClicks();
  if (!isBdkError(result)) {
    for (const click of result.clicks) {
      console.log(click.id, click.actionId, click.additionalData);
    }
  }
} catch {
  // Keep normal in-app navigation available.
}