※試すには、https接続可能なサーバが必要になります(localhostでも可能なようですが、大量の通知を試すには、誰からでもアクセス出来た方が良いと思うので)。
このfirebaseConfigの部分は、次のVAPID Keyと一緒に、ブラウザのToken登録画面で使用します。
コピーし忘れたら、設定>全般タブ>マイアプリ>ウェブアプリ>[アプリ名]からコピーできます。
これも、ブラウザのToken登録画面で使用します。
※firebaseConfigとVAPID_PUBKEYを、
Firebase SDKの追加で取得したfirebaseConfig、
鍵ペア作成(VAPID Key)で作成された値
に入れ替えて下さい。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.2" />
<title>FCM Token登録</title>
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.2.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/11.2.0/firebase-analytics.js";
import { getMessaging, getToken } from "https://www.gstatic.com/firebasejs/11.2.0/firebase-messaging.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
// Firebase SDKの追加で取得したfirebaseConfigで上書き
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
// 登録
const btn = document.getElementById('regist');
btn.onclick = function(){
console.log("clicked");
var el = document.getElementById("username");
if (!("Notification" in window)) {
// ブラウザーが通知に対応しているか調べる
alert("このブラウザーはデスクトップ通知には対応していません。");
}else if(el.value.length == 0){
alert("ユーザー名を入力して下さい。");
} else if (Notification.permission !== "denied") {
Notification.requestPermission().then((permission) => {
const vapidKey = "VAPID_PUBKEY"; // 鍵ペア作成(VAPID Key)で作成された値
const messaging = getMessaging();
try{
// Add the public key generated from the console here.
getToken(messaging, {vapidKey:vapidKey}).then((token) => {
console.log(token);
showToken(token);
});
}catch(e){
console.error(e);
}
});
} else {
alert("Notification permission is denied.");
}
}
function showToken(token){
document.getElementById("box").style.display = "block";
document.getElementById("result").innerHTML = token;
}
</script>
</head>
<body>
<form method="POST" id="form">
<button type="button" id="regist">Push通知登録</button>
</form>
<br>
<div id="box" style="display:none;">
<div>Token: </div>
<div id="result" style="border: solid 1px #CCCCCC; padding: 4px;"></div>
<div> </div>
</div>
</body>
</html>
※FirefoxはToken取得は出来ても、Push通知時にTHIRD_PARTY_AUTH_ERRORを起こす事が有るようです。
プロジェクトAでは全く発生せず、プロジェクトBでは必ず発生すると言う状況でした。
というかこの記事の検証用に追加したプロジェクトで起きて、調べまくりました。
一回目のプロジェクトでは問題なかったのに・・・。
https://stackoverflow.com/questions/72737181/how-do-i-fix-the-problem-behind-third-party-auth-error-when-using-googles-api
https://stackoverflow.com/questions/73218243/fcm-messages-not-being-sent-to-firefox
そんなわけで、こちらでは解決出来ないため、Chromeを使わせるしかない状況のようです。
Edgeはブラウザの設定でサイトの許可が必要でしたが、登録自体は可能でした。
iOSは制限が強く、簡単に使うためにブラウザを、と言う趣旨から外れるので除外しました。
結論Chrome(かEdge)使えになってしまうのは、仕方ないですね。
ブラウザ(OS) | Token登録 | Push通知 |
---|---|---|
Firefox (Windows) | 〇 | △(別プロジェクトで原因不明のエラー) |
Google Chrome (Windows) | 〇 | 〇 |
Edge (Windows) | △(ブラウザのセキュリティ設定が必要) | 〇 |
Chrome(Android) | 〇 | 〇 |
// Push通知を受け取ると呼ばれる
self.addEventListener('push', function (event) {
// メッセージを表示する
var data = {};
if (event.data) {
data = event.data.json();
}
var title = data.notification.title;
var message = data.notification.body;
event.waitUntil(
self.registration.showNotification(title, {
'body': message
})
);
});
gcloud auth login
# ブラウザが開くのでGoogleアカウントを選択してログイン
gcloud config set [project name]
gcloud auth print-access-token
curl -X POST \
-H "Authorization: Bearer [access_token]" \
-H "Content-Type: application/json" \
-d '{
"message": {
"token": "[browser_token]",
"notification": {
"title": "Push 通知のタイトル",
"body": "Push 通知の本文"
}
}
}' \
"https://fcm.googleapis.com/v1/projects/[project_id]/messages:send"
[access_token]をgcloud auth print-access-tokenで出力された値に、