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 and left unchanged.

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 change alert settings or the policy without recreating the subscription. Only the data portion (alerts and policy) can be changed.

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

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+)