1

I use this vuex action for registering my push notification token with my server:

  async submitPushToken({commit, getters}) {
    commit('SET_PUSH_TOKEN', await firebase.getCurrentPushToken());
    return api
      .post('/push/register/', {registration_id: getters.pushToken})
      .catch(e => alert(`You won't receive notifications because ${e}`));
  },

When should I register my push token, after user registration, after each login? After every update to application? When Does the token get updated itself?

yukashima huksay
  • 5,834
  • 7
  • 45
  • 78
  • It's really upto you, when you want to do it. In my opinion upon login would be good, unless you want to send notification to a user who hasn't logged in to app yet. – Manoj Apr 24 '20 at 19:39
  • @Manoj but when does the token change? – yukashima huksay Apr 25 '20 at 02:13
  • `getCurrentPushToken()` always gives you the current push token. So you could probably update the token when you validate user's stored credentials / auth token. – Manoj Apr 25 '20 at 09:44
  • @Manoj my point is that I don't want to send the token to server if the server already has it. I want to know when the current push token changes so I only send it to the server then. I know I could just check in my server if the token is the same as before but I would like to send the token only when it needs to be sent. – yukashima huksay Apr 25 '20 at 15:12
  • Then you will have to create a custom message service and use onNewToken callback which will be called whenever token is updated. – Manoj Apr 25 '20 at 20:02
  • @Manoj but doesn't it have a rule? don't we know when the token is renewed in firebase? how is the token generated? Why can't I know when firebase changes the token so I can only call getCurrentPushToken then? I don't understand. – yukashima huksay Apr 26 '20 at 06:10
  • Yes you can know when new token is created by implementing onNewToken - I suggest you to read https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService#onNewToken(java.lang.String) or if you want to keep it simple save old token in app settings, call get current token, compare both if they are different hit the API. – Manoj Apr 26 '20 at 14:39

0 Answers0