Send FCM registration immediately the app is installed
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
add a comment |
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
When are you storing the token is when in theonTokenRefresh
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
add a comment |
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
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
java php android mysql firebase
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 theonTokenRefresh
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
add a comment |
When are you storing the token is when in theonTokenRefresh
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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 aFirebaseMessagingService
subclass.
– Frank van Puffelen
Nov 13 '18 at 3:37
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 aFirebaseMessagingService
subclass.
– Frank van Puffelen
Nov 13 '18 at 3:37
add a comment |
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.
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 aFirebaseMessagingService
subclass.
– Frank van Puffelen
Nov 13 '18 at 3:37
add a comment |
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.
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.
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 aFirebaseMessagingService
subclass.
– Frank van Puffelen
Nov 13 '18 at 3:37
add a comment |
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 aFirebaseMessagingService
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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