Skip to main content

Push Notifications

The client.push API manages Web Push subscriptions. It follows the Web Push Protocol and allows your application to receive notifications in the background.

Subscribing​

Create a new Web Push subscription by providing your push endpoint and VAPID keys:

final subscription = await client.push.create(
MastodonPushSubscriptionRequest(
endpoint: 'https://push.example.com/subscription/abc123',
p256dh: '<Base64url-encoded P-256 ECDH public key>',
auth: '<Base64url-encoded authentication secret>',
standard: true,
alerts: MastodonPushAlertSettings(
mention: true,
follow: true,
reblog: true,
favourite: true,
poll: true,
),
policy: 'followed', // all | followed | follower | none
),
);

print(subscription.id);
print(subscription.serverKey); // use to verify incoming pushes

Alert types​

MastodonPushAlertSettings accepts any combination of the following fields. Fields set to null are omitted from the request. On creation, omitted fields use the server defaults. Updates replace the whole settings object as described below.

FieldDescription
mentionMentioned in a status
quoteStatus was quoted
statusNew post from a followed user
reblogStatus was boosted
followNew follower
followRequestNew follow request
favouriteStatus was favourited
pollA poll you voted in or created has ended
updateA status you interacted with was edited
quotedUpdateA quoted status was edited
adminSignUpNew user sign-up (admin only)
adminReportNew report (admin only)

Policy values​

ValueDescription
allReceive notifications from all users
followedReceive notifications only from users you follow
followerReceive notifications only from your followers
noneDisable all push notifications

Fetching the current subscription​

final subscription = await client.push.fetch();
print(subscription.endpoint);
print(subscription.alerts.mention);
print(subscription.policy);

Updating alert settings​

Use update to replace the subscription's data portion (alerts and policy) without recreating the subscription. This is not a partial update: every omitted alert type becomes disabled, and omitting policy resets it to all. Send every alert type that should remain enabled. A request with neither alerts nor policy throws ArgumentError before sending an HTTP request.

final updated = await client.push.update(
MastodonPushSubscriptionUpdateRequest(
alerts: MastodonPushAlertSettings(
mention: true,
follow: false,
reblog: true,
favourite: true,
poll: true,
),
policy: 'all',
),
);

To explicitly disable every alert and reset the policy to all, send an empty alert settings object:

await client.push.update(
const MastodonPushSubscriptionUpdateRequest(
alerts: MastodonPushAlertSettings(),
),
);

Unsubscribing​

await client.push.delete();

Deletes the current Web Push subscription. Subsequent calls to fetch will throw a MastodonNotFoundException.

The MastodonWebPushSubscription model​

FieldTypeDescription
idStringSubscription ID
endpointStringPush endpoint URL
serverKeyStringServer public key for verifying push messages
alertsMastodonPushAlertsActive alert settings
policyStringNotification policy
standardbool?Whether the subscription conforms to the standardized Web Push spec (Mastodon 4.4+)