Hello, I hope you’re all doing well
I managed to get pushed notifications on an emulator from firebase, registering on the app, getting the token, having my api prompt the firebase endpoint and back to the app again.
The thing I can’t quite understand and not finding clear material on is where do I put the code for adding listeners and registering for token.
For now I had them in my App.vue onMounted but now moved them to a button click (for testing),
If I do leave them in main.ts for example or the mounting of any page/component, wouldn’t every time the user opens the app or opens that page the app would run the code again and getting a different/same code needlessly?
Is that the way it’s supposed to be always getting a different token for each use and re-registering it in my api for this current user? Which I’m sure is not the case as it doesn’t make much sense.
I really could use some help on wrapping my head around this so I understand it better.
Thank you kindly, I hope you’re having a good day
The push notification token can change at any time, so it’s recommended to always call register on app startup to see if it changed.
The token is generated and provided by firebase, not sure if the SDK does it locally or it takes part on their servers.
Google recommends storing them in your server (not locally) with a timestamp, so you know last time the app was used.
Also when you send a notification from your server to multiple devices, google servers return you the tokens what are no longer available, so you have to remove those from your server to not try to send notifications to them in the future since they are now invalid.
I store it locally in the app and in the backend. Every time the app opens, I call PushNotifications.register() which then triggers the registration listener if successful. Within the listener, I check if the current token matches what is stored locally. I only send it to my backend (API) if the token is different/has changed or if my app hasn’t sent it to the backend in the last month.