问题
I'm trying to send a push notification to my app from my app server but i'm not getting the FCM registration token through FirebaseInstanceIdService
. Here i'm using Genymotion personal edition emulator.is that problem with my emulators API?
here is FirebaseInstanceIdService
public class FcmInstanceIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String recent_token = FirebaseInstanceId.getInstance().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();
}
} here is MainActivity
public class MainActivity extends AppCompatActivity {
String url = "http://myurl.fcm_insert.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button2);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
final String token = sharedPreferences.getString(getString(R.string.FCM_TOKEN),"");
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Response",error.toString());
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("token",token);
return params;
}
} ;
Singletone.getSingletone(MainActivity.this).addToRequest(stringRequest);
}
});
}
}
it's only sending the default value to the server.
回答1:
If you already added the service in your manifest file then you are right it's happening cause of you Genymotion emulators API, then Check this answer
回答2:
Main Problem is Uninstall first your apk app from your mobile then run and build again from android studio. It helps for me
If google play services are installed and you are still not getting the token then probably you have implemented your SharedPreferences
inside onTokenRefresh()
after the token has been generated. The problem would have been solved automatically if the Application is reinstalled. Remember the onTokenRefresh()
callback fires whenever a new token is generated. So if the token is already generated onTokenRefresh()
won't get called. Only when the token has changed onTokenRefresh()
method will get called. You can always get the generated token value by calling FirebaseInstanceId.getInstance().getToken();
almost anywhere in your Application. For your case only you can retrieve the token from SharedPreferences
as follows:-
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE); final String token = sharedPreferences.getString(getString(R.string.FCM_TOKEN),FirebaseInstanceId.getInstance().getToken());
This will ensure that you are always getting the token, and if the token changes onTokenRefresh()
will get called and your SharedPreferences
will get updated.
回答3:
It happen because Genymotion have not google play services by default.
回答4:
Follo the further instructions to install Google Services on Genymotion, it will take only 10 minutes only
How to install Google Play Services in a Genymotion VM (with no drag and drop support)?
来源:https://stackoverflow.com/questions/42581532/why-not-getting-android-firebase-fcm-registration-token