Send FCM registration immediately the app is installed










0














I have an app that stores an FCM registration token to a MySQL database. When the app is installed initially the app sends null to the database, but when the app is launched a second time the token is sent to the database.



Is there a way where the token can be sent immediately when the app is installed?



MainActivity



 FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>()
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task)
if (task.isSuccessful())

String recent_token = task.getResult().getToken();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(getString(R.string.FCM_TOKEN), recent_token);

editor.commit();

else




);
SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
final String token=sharedPreferences.getString(getString(R.string.FCM_TOKEN),"");









share|improve this question























  • When are you storing the token is when in the onTokenRefresh method
    – Mohammad C
    Nov 13 '18 at 3:27










  • I store the token in shared Preferences, Ive edited the question to include the code I use to generate FCM token
    – Neliswa Astute
    Nov 13 '18 at 3:30
















0














I have an app that stores an FCM registration token to a MySQL database. When the app is installed initially the app sends null to the database, but when the app is launched a second time the token is sent to the database.



Is there a way where the token can be sent immediately when the app is installed?



MainActivity



 FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>()
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task)
if (task.isSuccessful())

String recent_token = task.getResult().getToken();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(getString(R.string.FCM_TOKEN), recent_token);

editor.commit();

else




);
SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
final String token=sharedPreferences.getString(getString(R.string.FCM_TOKEN),"");









share|improve this question























  • When are you storing the token is when in the onTokenRefresh method
    – Mohammad C
    Nov 13 '18 at 3:27










  • I store the token in shared Preferences, Ive edited the question to include the code I use to generate FCM token
    – Neliswa Astute
    Nov 13 '18 at 3:30














0












0








0







I have an app that stores an FCM registration token to a MySQL database. When the app is installed initially the app sends null to the database, but when the app is launched a second time the token is sent to the database.



Is there a way where the token can be sent immediately when the app is installed?



MainActivity



 FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>()
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task)
if (task.isSuccessful())

String recent_token = task.getResult().getToken();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(getString(R.string.FCM_TOKEN), recent_token);

editor.commit();

else




);
SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
final String token=sharedPreferences.getString(getString(R.string.FCM_TOKEN),"");









share|improve this question















I have an app that stores an FCM registration token to a MySQL database. When the app is installed initially the app sends null to the database, but when the app is launched a second time the token is sent to the database.



Is there a way where the token can be sent immediately when the app is installed?



MainActivity



 FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>()
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task)
if (task.isSuccessful())

String recent_token = task.getResult().getToken();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(getString(R.string.FCM_TOKEN), recent_token);

editor.commit();

else




);
SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
final String token=sharedPreferences.getString(getString(R.string.FCM_TOKEN),"");






java php android mysql firebase






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 3:31

























asked Nov 13 '18 at 3:22









Neliswa Astute

365




365











  • When are you storing the token is when in the onTokenRefresh method
    – Mohammad C
    Nov 13 '18 at 3:27










  • I store the token in shared Preferences, Ive edited the question to include the code I use to generate FCM token
    – Neliswa Astute
    Nov 13 '18 at 3:30

















  • When are you storing the token is when in the onTokenRefresh method
    – Mohammad C
    Nov 13 '18 at 3:27










  • I store the token in shared Preferences, Ive edited the question to include the code I use to generate FCM token
    – Neliswa Astute
    Nov 13 '18 at 3:30
















When are you storing the token is when in the onTokenRefresh method
– Mohammad C
Nov 13 '18 at 3:27




When are you storing the token is when in the onTokenRefresh method
– Mohammad C
Nov 13 '18 at 3:27












I store the token in shared Preferences, Ive edited the question to include the code I use to generate FCM token
– Neliswa Astute
Nov 13 '18 at 3:30





I store the token in shared Preferences, Ive edited the question to include the code I use to generate FCM token
– Neliswa Astute
Nov 13 '18 at 3:30













1 Answer
1






active

oldest

votes


















2














The FCM token is generated asynchronously. Most like you're requesting the token through FirebaseInstanceId.getInstance().getInstanceId() when it hasn't been generated yet. To ensure you get the token as soon is it has been generated, monitor token generation as shown in the documentation:




/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
@Override
public void onNewToken(String token)
Log.d(TAG, "Refreshed token: " + token);

// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(token);




This ensures that you always get called whenever a token gets generated, which happens when the app first runs after installing, and may occur at certain moments after that.






share|improve this answer




















  • I should use this code in FirebaseMessagingService or in Main Activity?
    – Neliswa Astute
    Nov 13 '18 at 3:34











  • If you look at the sample in the documentation link I provided, it links out to the quickstart app that the code comes from: github.com/firebase/quickstart-android/blob/…. So that's in a FirebaseMessagingService subclass.
    – Frank van Puffelen
    Nov 13 '18 at 3:37










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273325%2fsend-fcm-registration-immediately-the-app-is-installed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The FCM token is generated asynchronously. Most like you're requesting the token through FirebaseInstanceId.getInstance().getInstanceId() when it hasn't been generated yet. To ensure you get the token as soon is it has been generated, monitor token generation as shown in the documentation:




/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
@Override
public void onNewToken(String token)
Log.d(TAG, "Refreshed token: " + token);

// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(token);




This ensures that you always get called whenever a token gets generated, which happens when the app first runs after installing, and may occur at certain moments after that.






share|improve this answer




















  • I should use this code in FirebaseMessagingService or in Main Activity?
    – Neliswa Astute
    Nov 13 '18 at 3:34











  • If you look at the sample in the documentation link I provided, it links out to the quickstart app that the code comes from: github.com/firebase/quickstart-android/blob/…. So that's in a FirebaseMessagingService subclass.
    – Frank van Puffelen
    Nov 13 '18 at 3:37















2














The FCM token is generated asynchronously. Most like you're requesting the token through FirebaseInstanceId.getInstance().getInstanceId() when it hasn't been generated yet. To ensure you get the token as soon is it has been generated, monitor token generation as shown in the documentation:




/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
@Override
public void onNewToken(String token)
Log.d(TAG, "Refreshed token: " + token);

// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(token);




This ensures that you always get called whenever a token gets generated, which happens when the app first runs after installing, and may occur at certain moments after that.






share|improve this answer




















  • I should use this code in FirebaseMessagingService or in Main Activity?
    – Neliswa Astute
    Nov 13 '18 at 3:34











  • If you look at the sample in the documentation link I provided, it links out to the quickstart app that the code comes from: github.com/firebase/quickstart-android/blob/…. So that's in a FirebaseMessagingService subclass.
    – Frank van Puffelen
    Nov 13 '18 at 3:37













2












2








2






The FCM token is generated asynchronously. Most like you're requesting the token through FirebaseInstanceId.getInstance().getInstanceId() when it hasn't been generated yet. To ensure you get the token as soon is it has been generated, monitor token generation as shown in the documentation:




/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
@Override
public void onNewToken(String token)
Log.d(TAG, "Refreshed token: " + token);

// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(token);




This ensures that you always get called whenever a token gets generated, which happens when the app first runs after installing, and may occur at certain moments after that.






share|improve this answer












The FCM token is generated asynchronously. Most like you're requesting the token through FirebaseInstanceId.getInstance().getInstanceId() when it hasn't been generated yet. To ensure you get the token as soon is it has been generated, monitor token generation as shown in the documentation:




/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
@Override
public void onNewToken(String token)
Log.d(TAG, "Refreshed token: " + token);

// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(token);




This ensures that you always get called whenever a token gets generated, which happens when the app first runs after installing, and may occur at certain moments after that.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 3:32









Frank van Puffelen

227k28373396




227k28373396











  • I should use this code in FirebaseMessagingService or in Main Activity?
    – Neliswa Astute
    Nov 13 '18 at 3:34











  • If you look at the sample in the documentation link I provided, it links out to the quickstart app that the code comes from: github.com/firebase/quickstart-android/blob/…. So that's in a FirebaseMessagingService subclass.
    – Frank van Puffelen
    Nov 13 '18 at 3:37
















  • I should use this code in FirebaseMessagingService or in Main Activity?
    – Neliswa Astute
    Nov 13 '18 at 3:34











  • If you look at the sample in the documentation link I provided, it links out to the quickstart app that the code comes from: github.com/firebase/quickstart-android/blob/…. So that's in a FirebaseMessagingService subclass.
    – Frank van Puffelen
    Nov 13 '18 at 3:37















I should use this code in FirebaseMessagingService or in Main Activity?
– Neliswa Astute
Nov 13 '18 at 3:34





I should use this code in FirebaseMessagingService or in Main Activity?
– Neliswa Astute
Nov 13 '18 at 3:34













If you look at the sample in the documentation link I provided, it links out to the quickstart app that the code comes from: github.com/firebase/quickstart-android/blob/…. So that's in a FirebaseMessagingService subclass.
– Frank van Puffelen
Nov 13 '18 at 3:37




If you look at the sample in the documentation link I provided, it links out to the quickstart app that the code comes from: github.com/firebase/quickstart-android/blob/…. So that's in a FirebaseMessagingService subclass.
– Frank van Puffelen
Nov 13 '18 at 3:37

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273325%2fsend-fcm-registration-immediately-the-app-is-installed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党