Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.evollu.react.fcm;
- 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.messaging.FirebaseMessagingService;
- public class InstanceIdService extends FirebaseMessagingService {
- private static final String TAG = "InstanceIdService";
- /**
- * Called if InstanceID token is updated. This may occur if the security of
- * the previous token had been compromised. This call is initiated by the
- * InstanceID provider.
- */
- @Override
- public void onNewToken(String s) {
- super.onNewToken(s);
- String refreshedToken = FirebaseInstanceId.getInstance().getToken();
- Log.d(TAG, "Refreshed token: " + refreshedToken);
- // Broadcast refreshed token
- final 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);
- 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(i);
- } else {
- // Otherwise wait for construction, then send the notification
- mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
- public void onReactContextInitialized(ReactContext context) {
- LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
- }
- });
- if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
- // Construct it in the background
- mReactInstanceManager.createReactContextInBackground();
- }
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement