PushEvent
        
        
          
                Baseline
                
                  Widely available
                
                
              
        
        
        
          
                
              
                
              
                
              
        
        
      
      This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2023年3月.
PushEvent はプッシュ API のインターフェイスで、受け取ったプッシュメッセージを表します。このイベントは、ServiceWorker の グローバルスコープに送信されます。これは、アプリケーションサーバーから PushSubscription に送信された情報を含みます。
コンストラクター
PushEvent()- 
新しい
PushEventオブジェクトを生成します。 
インスタンスプロパティ
親である ExtendableEvent からプロパティを継承しています。追加プロパティ:
PushEvent.data読取専用- 
PushSubscriptionに送信されたデータを含むPushMessageDataオブジェクトへの参照を返します。 
インスタンスメソッド
親である ExtendableEvent からメソッドを継承しています。
例
次の例は、PushEvent からデータを取得して、すべてのサービスワーカークライアントで表示しています。
js
self.addEventListener("push", (event) => {
  if (!(self.Notification && self.Notification.permission === "granted")) {
    return;
  }
  const data = event.data?.json() ?? {};
  const title = data.title || "Something Has Happened";
  const message =
    data.message || "Here's something you might want to check out.";
  const icon = "images/new-notification.png";
  const notification = new self.Notification(title, {
    body: message,
    tag: "simple-push-demo-notification",
    icon,
  });
  notification.addEventListener("click", () => {
    clients.openWindow(
      "https://example.blog.com/2015/03/04/something-new.html",
    );
  });
});
仕様書
| Specification | 
|---|
| Push API> # pushevent-interface>  | 
            
ブラウザーの互換性
Loading…