chris-schmitz
11/16/2022, 1:55 PM<script type="module">
import { initializeApp } from "<https://www.gstatic.com/firebasejs/9.14.0/firebase-app.js>";
import { getMessaging, getToken, onMessage } from "<https://www.gstatic.com/firebasejs/9.14.0/firebase-messaging.js>";
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "<Api-Key>",
authDomain: "<Domain>",
projectId: "<ProjectId>",
storageBucket: "<Bucket>",
messagingSenderId: "<Sender-Id>",
appId: "<App-Id>",
name: "<App-Name>"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const messaging = getMessaging();
getToken( messaging, { vapidKey: '<Public-Key>' } )
.then((currentToken) => {
if (currentToken) {
window.localStorage.setItem( 'fbToken', currentToken );
} else {
console.log('No registration token available. Request permission to generate one.');
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
});
onMessage(messaging, payload => {
console.log("Message received. ", payload);
const { title, ...options } = payload.notification;
});
</script>
On clicking a button the app reads the token from local storage and sends an ajax request to CF which then uses cfhttp()
to send a message to Firebase with this request body:
{
"to" : arguments.data.fbToken
, "notification" : {
"message": arguments.data.msg
, "title": "Greetings"
}
}
The result of the cfhttp()
call shows success: 1
(plus things like multicast_id
and message_id
), so I assume that I did things right so far.
Yet the onMessage()
event handler never fires, and I don't know whether the message never arrives or if there is a different problem.
Any ideas or hints?salted
12/01/2022, 6:20 PMsalted
12/01/2022, 6:23 PMsalted
12/01/2022, 6:23 PMfirebase.messaging.onMessage()...
salted
12/01/2022, 6:24 PMsalted
12/01/2022, 6:25 PMmessaging.onMessage
will do it