Skip to content

Commit

Permalink
broadcast refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
Libin Lu authored Feb 8, 2018
1 parent 4d5254f commit c63281c
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions android/src/main/java/com/evollu/react/fcm/InstanceIdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactContext;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

Expand All @@ -25,11 +30,35 @@ public void onTokenRefresh() {
Log.d(TAG, "Refreshed token: " + refreshedToken);

// Broadcast refreshed token

Intent i = new Intent("com.evollu.react.fcm.FCMRefreshToken");
Bundle bundle = new Bundle();
bundle.putString("token", refreshedToken);
i.putExtras(bundle);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);

final Intent message = i;

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
// Construct and load our normal React JS code bundle
ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
ReactContext context = mReactInstanceManager.getCurrentReactContext();
// If it's constructed, send a notification
if (context != null) {
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message);
} else {
// Otherwise wait for construction, then send the notification
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
public void onReactContextInitialized(ReactContext context) {
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message);
}
});
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
// Construct it in the background
mReactInstanceManager.createReactContextInBackground();
}
}
}
});
}
}

0 comments on commit c63281c

Please sign in to comment.