-2

I want to send data (String) from service to Activity. How can I do that? this is my service and I want to send the token to RegisterActivity, but it doesn't work

public class FirebaseInstanceIDService extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {

        String token = FirebaseInstanceId.getInstance().getToken();

        Log.d("My firebase id", "Refreshed token: " + token);

        Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
        intent.putExtra("TokenValue", token);
        FirebaseInstanceIDService.this.startActivity(intent );

    }

In RegisterActivity

 Intent intent = getIntent();
            String tokenValue = intent.getStringExtra("TokenValue");
            Toast.makeText(RegisterActivity.this,tokenValue,Toast.LENGTH_SHORT).show();
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mehdi
  • 1
  • 6

3 Answers3

0

Add flag FLAG_ACTIVITY_NEW_TASK in your intent before calling startActivity.

Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("TokenValue", token);
startActivity(intent);
Krishna Sharma
  • 2,828
  • 1
  • 12
  • 23
0

using interface it can be done easily.first create a interface and then subscribe to this interface from activity or fragment from where you want to get the text.and then broadcast data from service

0

Try this:

This is my service class like this:

public class TokenService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
public static String DeviceToc = "";
@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.e(TAG, "Refreshed token: " + refreshedToken);

    DeviceToc = refreshedToken;
    Log.e("DeviceToc",""+refreshedToken);
    sendRegistrationToServer(refreshedToken);
}

private void sendRegistrationToServer(String token) {
  //        Utils.storeUserPreferences(this, DeviceToc,token);
    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);

    SharedPreferences.Editor editor = pref.edit();
    editor.putString("deviceToc",token); // Storing string
    editor.apply();

    Log.i("token",""+token);
    // Add custom implementation, as needed.
}

And this is my login activity get token using Shared preference like this

      @Override
        protected Map<String, String> getParams()
        {
            Context c=LoginActivity.this;
            SharedPreferences pref = c.getApplicationContext().getSharedPreferences("MyPref", 0);
            String device_tok = pref.getString("deviceToc", null);
            //Pass the parameters to according to the API.
            Map<String, String> params = new HashMap<String, String>();
            params.put("user_name", emailedit.getText().toString().trim());
            params.put("userDeviceToken",device_tok);
            params.put("deviceType","android");
            params.put("user_login_password", passwordedit.getText().toString().trim());
            Log.d("params",""+params);

it helps you.

Android Geek
  • 8,956
  • 2
  • 21
  • 35