I'm trying to create a small PWA with JavaScript (...
# javascript
c
I'm trying to create a small PWA with JavaScript (just jQuery) that uses Firebase Cloud Messaging. I Initialize messaging like this:
Copy code
<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:
Copy code
{
    "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?
s
From your example onmessage is a global method? Shouldn't it be attached to your request
or you have to call firebase to process it
i.e.
firebase.messaging.onMessage()...
if it's firebase that's listening it won't fire at all called globally like your example
or possibly just
messaging.onMessage
will do it