LoginSignup
2
0

More than 5 years have passed since last update.

Notification APIメモ

Last updated at Posted at 2019-03-28

通知したくなったので調べた

まずユーザーがブラウザ通知許可しているか調べる

    if (Notification.permission !== 'denied') {

    }

上記のif文で判定できる

他の判定

MDNから引用

default
ユーザはまだ許可を求められておらず、通知は表示されません。
granted
ユーザは以前に通知表示の許可を求められており、許可しました。
denied
ユーザは、通知の表示を明示的に拒否しました

次はユーザーに通知を許可させる

    if (Notification.permission !== 'denied') {
      Notification.requestPermission(function (permission) {
      });
    }

これで準備完了これで通知できるようになったので

var Notification = new Notification(theTitle,options);

タイトルだけなら引数に入れるだけで表示できる

function spawnNotification(theBody,theIcon,theTitle) {
  var options = {
      body: theBody,
      icon: theIcon
  }
  var n = new Notification(theTitle,options);
}

このoptionsで一番面白そうなのがvibrate

通知が発火したときにデバイスのバイブレーションハードウェアに通知するバイブレーションパターンです。

今の所Android端末のみをバイブさせれるみたい
試してみたい
iosもこないかな...

これのwrapper
https://pushjs.org/
正直カスタマイズ性にかけるので
Notification API使ったほうが良い

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0