androidStudioのエミュレータへfcmを使ってwebプッシュを送信したいです。
pcのchromeへのwebプッシュは成功しています。
以下、コードです。
firebase-messaging-sw.js
javascript
1 2importScripts("https://www.gstatic.com/firebasejs/7.14.4/firebase-app.js"); 3importScripts( 4 "https://www.gstatic.com/firebasejs/7.14.4/firebase-messaging.js" 5); 6 7// Initialize the Firebase app in the service worker by passing in 8// your app's Firebase config object. 9// https://firebase.google.com/docs/web/setup#config-object 10firebase.initializeApp({ 11 apiKey: "AIzaSyCJzLFZEiMIzg1EKt5HzSUZb4Ly_gsQDTk", 12 authDomain: "pwatest-a13b8.firebaseapp.com", 13 databaseURL: "https://pwatest-a13b8.firebaseio.com", 14 projectId: "pwatest-a13b8", 15 storageBucket: "pwatest-a13b8.appspot.com", 16 messagingSenderId: "165204440072", 17 appId: "1:165204440072:web:0e5dfacb7bb4320b288dbd", 18}); 19 20// Retrieve an instance of Firebase Messaging so that it can handle background 21// messages. 22const messaging = firebase.messaging(); 23messaging.setBackgroundMessageHandler(function (payload) { 24 console.log( 25 "[firebase-messaging-sw.js] Received background message ", 26 payload 27 ); 28 // Customize notification here 29 const notificationTitle = "Background Message Title"; 30 const notificationOptions = { 31 body: "Background Message body.", 32 icon: "/firebase-logo.png", 33 }; 34 35 return self.registration.showNotification( 36 notificationTitle, 37 notificationOptions 38 ); 39}); 40
index.js
javascript
1import firebase from "firebase/app"; 2import "firebase/messaging"; 3import "firebase/firestore"; 4 5var firebaseConfig = { 6 apiKey: "AIzaSyCJzLFZEiMIzg1EKt5HzSUZb4Ly_gsQDTk", 7 authDomain: "pwatest-a13b8.firebaseapp.com", 8 databaseURL: "https://pwatest-a13b8.firebaseio.com", 9 projectId: "pwatest-a13b8", 10 storageBucket: "pwatest-a13b8.appspot.com", 11 messagingSenderId: "165204440072", 12 appId: "1:165204440072:web:0e5dfacb7bb4320b288dbd", 13}; 14// Initialize Firebase 15firebase.initializeApp(firebaseConfig); 16 17const messaging = firebase.messaging(); 18messaging.usePublicVapidKey( 19 "BANkA5IJWmPZ68TtZP7mtG9Ts7IokiWGdPzXyoWaUA5AVkt7gx2ZmkRv7YGdjj9UNbQsWEzgpIH8UN9hKOFpUDc" 20); 21 22var db = firebase.firestore(); 23var usersRef = db.collection("users"); 24 25navigator.permissions.query({ name: "notifications" }).then((status) => { 26 if (status.state === "granted") console.log("許可済み"); 27 else if (status.state === "prompt") console.log("ユーザ確認待ち"); 28 // if (status.state === 'denied') 29 else console.log("拒否済み"); 30}); 31 32messaging.requestPermission().then(function () { 33 messaging 34 .getToken() 35 .then((currentToken) => { 36 if (currentToken) { 37 console.log("ok"); 38 } else { 39 // Show permission request. 40 console.log( 41 "No Instance ID token available. Request permission to generate one." 42 ); 43 } 44 }) 45 .catch((err) => { 46 console.log("An error occurred while retrieving token. ", err); 47 }); 48}); 49 50messaging.onMessage((payload) => { 51 console.log("Message received. ", payload); 52 // ... 53}); 54
manifest.json
json
1{ 2 "name": "ServiceWorker Push", 3 "short_name": "ServiceWorker Push", 4 "icons": [{ 5 "src": "./image/black.png", 6 "sizes": "192x192", 7 "type": "image/png" 8 }], 9 "start_url": "./index.html", 10 "display": "standalone", 11 "gcm_sender_id": "103953800507" 12}
navigator.permissions.query({ name: "notifications" })で確認したところ"granted"許可済みになっています。
chromeのdevToolのserviceWorckerのpushから送信してみてもダメでした。これはなぜかpcのchromeでも動作しません。
index.jsのmessaging.onMessageのconsole.logは出力されているのでfcmからpushを受け取れてはいると思うのですが、androidの通知が出てきません。
何が原因かご教授ください。
あなたの回答
tips
プレビュー