メインコンテンツまでスキップ

プッシュ通知(Push Notifications)

client.push API は Web Push サブスクリプションを管理します。Web Push プロトコルに準拠しており、アプリケーションがバックグラウンドで通知を受け取れるようにします。

サブスクリプションの作成

プッシュエンドポイントと VAPID キーを指定して、新しい Web Push サブスクリプションを作成します:

final subscription = await client.push.create(
MastodonPushSubscriptionRequest(
endpoint: 'https://push.example.com/subscription/abc123',
p256dh: '<Base64url エンコードされた P-256 ECDH 公開鍵>',
auth: '<Base64url エンコードされた認証シークレット>',
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); // プッシュメッセージの検証に使用

通知タイプ

MastodonPushAlertSettings には以下のフィールドを任意の組み合わせで指定できます。null のフィールドはリクエストから除外され、変更されません。

フィールド説明
mention投稿でメンションされた
quote投稿が引用された
statusフォローしているユーザーが新規投稿した
reblog投稿がブーストされた
follow新しいフォロワーが増えた
followRequestフォローリクエストが届いた
favourite投稿がお気に入りされた
poll投票したまたは作成したアンケートが終了した
update操作した投稿が編集された
quotedUpdate引用した投稿が編集された
adminSignUp新規ユーザーが登録した(管理者専用)
adminReport新しい通報が届いた(管理者専用)

ポリシー値

説明
allすべてのユーザーからの通知を受け取る
followedフォローしているユーザーからの通知のみ受け取る
followerフォロワーからの通知のみ受け取る
noneプッシュ通知をすべて無効にする

現在のサブスクリプションの取得

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

通知設定の更新

サブスクリプションを再作成せずに通知設定やポリシーを変更するには update を使います。変更できるのは data 部分(alerts と policy)のみです。

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

サブスクリプションの解除

await client.push.delete();

現在の Web Push サブスクリプションを削除します。解除後に fetch を呼び出すと MastodonNotFoundException がスローされます。

MastodonWebPushSubscription モデル

フィールド説明
idStringサブスクリプション ID
endpointStringプッシュエンドポイント URL
serverKeyStringプッシュメッセージ検証用のサーバー公開鍵
alertsMastodonPushAlerts有効な通知設定
policyString通知ポリシー
standardbool?標準化された Web Push 仕様に準拠しているか(Mastodon 4.4 以降)