Understanding push notifications code structure

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.

For the listeners it depends in your app, some people only want to listen in certain cases, but if you want them to always fire you have to put them on app startup too, javascript code execution doesn’t persist after the app is killed, despite the native code for the listeners will still be fired, if there is no javascript code listening for them your javascript code will not be notified.

1 Like

Thank you for your reply,

I see, Should I store the token locally and check every time I load the app, If the registration differs, I update my api with the new one?

Also, is the token generated by the device or is it because of the firebase config and the plugin automates all that noise but eventually gets a token from firebase?

Excuse the questions, this is my first time working with firebase and cloud messaging, Thank you Julio

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.

1 Like

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.

1 Like

Oh man this is gold, Thank you both very much this would’ve taken me too long to figure out how to do properly.
Have a good day fellas

1 Like