Closed rmilejcz closed 1 year ago
here is my service worker:
importScripts(
'https://www.gstatic.com/firebasejs/9.0.0/firebase-app-compat.js',
);
importScripts(
'https://www.gstatic.com/firebasejs/9.0.0/firebase-messaging-compat.js',
);
// Initialize the Firebase app in the service worker by passing the generated config
var firebaseConfig = {
apiKey: '<APIKEY>',
authDomain: 'kalos-services-push.firebaseapp.com',
databaseURL: 'https://kalos-services-push.firebaseio.com',
projectId: 'kalos-services-push',
storageBucket: 'kalos-services-push.appspot.com',
messagingSenderId: '493739388494',
appId: '1:493739388494:web:a89c4a4ab2d05131381d1f',
};
var app = firebase.initializeApp(firebaseConfig);
var messaging = firebase.messaging(app);
messaging.onBackgroundMessage(function (payload) {
console.log('Received background message ', payload);
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
};
self.registration
.showNotification(notificationTitle, notificationOptions)
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});
});
and here is the initialization of firebase in my webapp:
const firebaseConfig = {
apiKey: '<APIKEY>',
authDomain: 'kalos-services-push.firebaseapp.com',
databaseURL: 'https://kalos-services-push.firebaseio.com',
projectId: 'kalos-services-push',
storageBucket: 'kalos-services-push.appspot.com',
messagingSenderId: '493739388494',
appId: '1:493739388494:web:a89c4a4ab2d05131381d1f',
};
const registration = await registerServiceWorker(
'app/assets/js/firebase-messaging-sw.js',
);
const app = initializeApp(firebaseConfig);
const messaging = getMessaging(app);
onMessage(messaging, payload => {
console.log('recieved message: ', payload);
});
Hi @rmilejcz do you get any errors from the messaging API?
Hey @chong-shao any thoughts on what could be wrong here? Thanks! Could also be related to: #506
Hi thanks for your response, I do not receive any error, the return value for the error is always nil
. Also unfortunately I have tested with chrome, firefox, and safari, and I am unable to receive web push on any of them.
Also the notifications show as sent within my firebase console, though I still do not receive them on the web app. I have tried with both the web app in the foreground and the background.
Hi I was able to solve the issue by using specifying some Webpush properties, specifically TTL
and Urgency
:
&messaging.Message{
Notification: &messaging.Notification{Title: "title", Body: "body"},
Token: "FCM TOKEN",
Webpush: &messaging.WebpushConfig{
Headers: map[string]string{"Urgency":"high", "TTL": "4500"},
},
Thank you so much for your time, I hope this helps someone in the future.
When using the firebase SDK, push notifications that target devices on my web app are not received. Using the same information, I can successfully trigger push notifications to my web app from the google console.
Steps to reproduce:
FIrst you would need to setup a client to receive messages: https://firebase.google.com/docs/cloud-messaging/js/client
Add firebase to your server following these steps: https://firebase.google.com/docs/admin/setup
Make sure you are properly authenticated: https://firebase.google.com/docs/cloud-messaging/auth-server
Finally, follow these steps to send a message: https://firebase.google.com/docs/cloud-messaging/send-message
Relevant Code:
The result of the call is always
projects/kalos-services-push/messages/be6f7922-32ec-42e0-8ecd-9640adeef062
. When I use the exact same FCM registration token via the firebase console, the notification is delivered successfully. What did I miss?