LoginSignup
1
1

More than 1 year has passed since last update.

ライブラリなしでプッシュ通知を自作する

Last updated at Posted at 2022-08-16

ポートフォワーディングができなかったり、ブラウザもインストールできないときに使えます。

通知を待つ

音を鳴らすことも可能です。

Windows

$HttpWebRequest = [System.Net.HttpWebRequest]::Create('https://rppico-default-rtdb.asia-southeast1.firebasedatabase.app/msg.json')
$HttpWebRequest.Accept = 'text/event-stream'
$ResponseStream = $HttpWebRequest.GetResponse().GetResponseStream()
$StreamReader = [System.IO.StreamReader]::new($ResponseStream)

while (($line = $StreamReader.ReadLine()) -ne $null)
{    
    if (-! $line.StartsWith('data: {')){ continue }
    $ToastText01 = [Windows.UI.Notifications.ToastTemplateType, Windows.UI.Notifications, ContentType = WindowsRuntime]::ToastText01
    $TemplateContent = [Windows.UI.Notifications.ToastNotificationManager,Windows.UI.Notifications, ContentType = WindowsRuntime]::GetTemplateContent($ToastText01)
    $TemplateContent.SelectSingleNode('//text[@id="1"]').InnerText = ($line.Substring(6) | ConvertFrom-Json).data
    $AppId = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppId).Show($TemplateContent)
}

$StreamReader.Close() > $null
$ResponseStream.Close() > $null

Mac

curl https://rppico-default-rtdb.asia-southeast1.firebasedatabase.app/msg.json -H Accept:text/event-stream --no-buffer \
  | grep '^data: {' --line-buffered \
  | while read line; do osascript -e 'display notification "'"${line:26:-2}"'"'; done

Linux

wget --header Accept:text/event-stream -qO- https://rppico-default-rtdb.asia-southeast1.firebasedatabase.app/msg.json \
  | grep '^data: {' --line-buffered \
  | while read line; do notify-send "${line:26:-2}"; done

通知を送る

好きなメッセージを送ることもできます。

Windows

Invoke-WebRequest -Method PUT -Body ([System.Text.Encoding]::UTF8.GetBytes("`"🕊$(date -Format yyyy-MM-ddTHH:mm:ss.fff)`"")) https://rppico-default-rtdb.asia-southeast1.firebasedatabase.app/msg.json

Mac

curl -X PUT -d '"🐥'$(date --iso-8601=ns)'"' https://rppico-default-rtdb.asia-southeast1.firebasedatabase.app/msg.json

Linux

wget --method PUT --body-data '"🐧'$(date --iso-8601=ns)'"' -qO- https://rppico-default-rtdb.asia-southeast1.firebasedatabase.app/msg.json

エンドポイント

Firebase コンソールを開いて、URLをコピーします。

Screenshot from 2022-08-14 02-15-45

image

参考

1
1
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
1
1