JavaScript, Notification APIを使ってトースト通知をする、その2
はじめに
前回のNotificationのインスタンスを作成した後のオブジェクトはどうするんだ?と気になったので調べた。
動作環境
- Web Browser
- Google Chrome
- 81.0.4044.113(Official Build)(64bit)
- Google Chrome
開発環境
- Editor
- Visual Studio Code
- 1.44.2
- Visual Studio Code
イベントを設定できる
const title = "Good day!"; const options = { body : "It's a wonderful day!?", icon : "../Image/Icon/onsen64.png" }; const notification = new Notification(title, options); notification.onclick = function(event) { console.log("クリックしましたね!"); }; notification.onshow = function(event) { console.log("表示しましたね!"); }; notification.onclose = function(event) { console.log("閉じましたね!"); }; notification.onerror = function(event) { console.log("エラーです!"); };
onerror
は動作未確認。
onclick
内でthrow "sample"
を実行したけど発火しなかった。
おまけ、クリックイベント時のtimestampを得る
notification.onclick = function(event) { console.log(new Date(this.timestamp)); };
最初はoptions
のdata
プロパティにnew Date()
を仕込んでいたけど、
console.log(this);
の表示を見ていたらtimestamp
プロパティの存在に気づいた。
一部のブラウザでは未対応なので注意が必要か。
おまけ、雑記
dir
プロパティは指定してみたが変化なしsilent
プロパティはtrue
にしたら通知音が消えた