firebase / firebase/quickstart-js

firebase.messaging.getToken() seems to different for the window and the service worker, why?

Open
#220 0 comments 0 reactions 0 assignees View on GitHub
api: messaging
Dominant language
TypeScript
Stars
5.4k
Forks
3.7k
Avg merge
2d 13h
Merged PRs (30d)
6

Description

Hello,

I recently ran into this issue. I was attempting to get the current token for the user on the service worker.

My code on the service worker:

`firebase.initializeApp({
'messagingSenderId': '-'
})

let messaging = firebase.messaging();
let token = await messaging.getToken()
console.log(token);
`

My Code on the prompt event in the windowed webpage:
`var config = {
apiKey: "-",
authDomain: "-",
databaseURL: "-",
projectId: "-",
storageBucket: "",
messagingSenderId: "-"
};
firebase.initializeApp(config);
messaging.requestPermission()
.then(function () {
getCurrentToken();
}
//After user allows notifications
function getCurrentToken(){
// Retrieve Firebase Messaging object.
const messaging = firebase.messaging();
messaging.getToken()
.then(function (currentToken) {
console.log(currentToken);
}
}
`

Both calls gives me different

The interesting thing is if I use either token to send the notification to the client, it also works.

My question is why is there two different tokens from the initial allow event to the service worker on receive event. Is there something I am doing incorrectly? or is this an intended behavior.

Thanks in advance for your response.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.