Push notifications strategy on logout

What is the proper way to implement push notifications when you log out? I am still getting push notifications so trying to figure out what to do? Remove the tokens from the db? Tell the app to stop receiving pushes? I thought I had that figured out and await PushNotifications.removeAllListeners(); didn’t seem to work. What is everyone else doing?

And since I’m on that note, I am using Ionic/Vue and putting my logic for push notifications in App.vue. Is that the right place? Should it be in main.ts?

Yeah, I think you would need to remove their token from the DB. I store the token on the API token record so when they log out, it by association gets removed automatically.

I include my logic in main.ts via bootstap files and is called before createApp.


// Bootstrap

const app = createApp(App).use(IonicVue).use(router).use(store)

The problem for me is when I remove the push tokens from the db, when they login again and register it doesn’t always tell me their token.

Huh, interesting. Here is what I do.

In my bootstrap code (ran every time the app is mounted) I do the following:

  1. Initialize push notifications
    1. Clear listeners
    2. Add listeners back
    3. Add channels for Android
  2. If user is logged in, I run some update token code
    • If push notifications were granted permission, call register
    • If successful, it triggers the registration event which then handles sending the token to my API. I send an API call to update in the backend if the new token is different from what is stored locally, or if it hasn’t been updated for more than 4 weeks. This logic follows Firebase’s recommendations.

If the user wasn’t logged in while the bootstrap code runs, then I run the update token code logic during the login process.

If the user hasn’t granted permission yet, I have a modal that pops up after login that runs the request permission and register logic.

Oh wow, and you have vuex working in your project? You wouldn’t happen to have a boilerplate project would you? Interested in seeing how someone else does it.

My Vuex set up is pretty standard. Getting it fully typed with TypeScript is another story :slight_smile: I don’t have a boilerplate project. Is there something specific you are interested in? If I were to do it again, I would now use Pinia instead of Vuex. Pinia is the next generation store for Vue and is now the recommended way by Vue.