Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle FirebaseInstanceId.getInstance().getToken() = null

I have just migrated to FCM. I have added my class that extends from FirebaseInstanceIdService to receive a refreshedToken as and when appropriate.

My question is specific to the case when user installs my app first time and due to some reason, unable to receive a registration Id from onTokenRefresh. How are we supposed to handle this? Can I set a broadcast receiver from my FirebaseInstanceIdService class which will notify the Main activity when a registration Id is received?

like image 982
Lucky Avatar asked May 30 '16 04:05

Lucky


4 Answers

  • if your device have no connection to the internet onTokenRefresh() is never called and you should notify to user his/her device has no internet connection
  • firebase has its own network change listener and when a device connected to the internet then try to get token and return it, at this time you can tell your main activity by sending a local broadcast receiver that registration token is received.

use below codes:

    @Override
public void onTokenRefresh() {

    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();

    Log.d("FCN TOKEN GET", "Refreshed token: " + refreshedToken);

    final Intent intent = new Intent("tokenReceiver");
    // You can also include some extra data.
    final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
    intent.putExtra("token",refreshedToken);
    broadcastManager.sendBroadcast(intent);


}

in your main activity:

    public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
   LocalBroadcastManager.getInstance(this).registerReceiver(tokenReceiver,
            new IntentFilter("tokenReceiver"));

}

BroadcastReceiver tokenReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String token = intent.getStringExtra("token");
        if(token != null)
        {
            //send token to your server or what you want to do
        }

    }
};

}
like image 124
Edalat Feizi Avatar answered Nov 15 '22 05:11

Edalat Feizi


Change this in manifest.xml file tools:node="replace" to tools:node="merge".

like image 45
Azeez Avatar answered Nov 15 '22 05:11

Azeez


As far as I know, token will be null only when you try to run your app on emulator on which google play service is not there and when you are using dual email id on you google play store(on you actual device), but only one email id is verified for the usage. Those are the cases which will give you null token and I have already implemented FCM in my new project. So for rest of any cases , token won't be null.

like image 6
Mrugesh Thaker Avatar answered Nov 15 '22 07:11

Mrugesh Thaker


Use this class extends with..

public class MyFirebaseInstanceIDService  extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";
    public static final String REGISTRATION_SUCCESS = "RegistrationSuccess";


    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        Toast.makeText(MyFirebaseInstanceIDService.this,refreshedToken,Toast.LENGTH_LONG).show();
    }
}
like image 2
Munna Sharma Avatar answered Nov 15 '22 07:11

Munna Sharma