title: Firebase FCM token - When to send to server?
body: <p>Okay so I have an app which on first start takes you through a few welcoming slides, then takes you to a login/register page and then to <code>MainActivity</code>.</p>
  
  <p>I have just implemented <code>FCM</code> and the services generate a <code>token</code> <strong>before</strong> any of those pages have been seen by the user. How could I make it so that the service runs <strong>after</strong> I get to <code>MainActivity</code>?</p>
  
  <p>The problem is I'm trying to send the token as soon as it is refreshed to the <code>MySQL DB</code> to the appropriate user account, but since the user hasn't signed in yet, that is <code>null</code> and my message to the server fails. What's a good way to design this? I thought of saving the token in <code>SharedPreferences</code> and sending it to the server after the user has logged in but that creates lots of complications when the token is refreshed at some later point?!</p>
  
  <p><strong>Possible solution</strong>: </p>
  
  <p>I'm not sure I completely understand how the 2 services run but say in <code>onTokenRefresh</code> I just save the token into <code>SharedPreferences</code> and in MainActivity I get the value from <code>SP</code> and then I send it to the server. In that case when the token is refreshed the new value will immediately go into <code>SharedPreferences</code> again. But I would still need to check if it's a new value in SP and then reupload it to the server. This is confusing!</p>