Search code examples
androidfirebasefirebase-cloud-messaginggenymotion

Why not getting Android Firebase FCM registration token?


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.


Solution

  • 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