Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. package com.evollu.react.fcm;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Looper;
  7. import android.support.v4.content.LocalBroadcastManager;
  8. import android.util.Log;
  9.  
  10. import com.facebook.react.ReactApplication;
  11. import com.facebook.react.ReactInstanceManager;
  12. import com.facebook.react.bridge.ReactContext;
  13. import com.google.firebase.iid.FirebaseInstanceId;
  14. import com.google.firebase.messaging.FirebaseMessagingService;
  15.  
  16. public class InstanceIdService extends FirebaseMessagingService {
  17.  
  18. private static final String TAG = "InstanceIdService";
  19.  
  20. /**
  21. * Called if InstanceID token is updated. This may occur if the security of
  22. * the previous token had been compromised. This call is initiated by the
  23. * InstanceID provider.
  24. */
  25.  
  26. @Override
  27. public void onNewToken(String s) {
  28. super.onNewToken(s);
  29. String refreshedToken = FirebaseInstanceId.getInstance().getToken();
  30. Log.d(TAG, "Refreshed token: " + refreshedToken);
  31.  
  32. // Broadcast refreshed token
  33.  
  34. final Intent i = new Intent("com.evollu.react.fcm.FCMRefreshToken");
  35. Bundle bundle = new Bundle();
  36. bundle.putString("token", refreshedToken);
  37. i.putExtras(bundle);
  38. // LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
  39. Handler handler = new Handler(Looper.getMainLooper());
  40. handler.post(new Runnable() {
  41. public void run() {
  42. // Construct and load our normal React JS code bundle
  43. ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
  44. ReactContext context = mReactInstanceManager.getCurrentReactContext();
  45. // If it's constructed, send a notification
  46. if (context != null) {
  47. LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
  48. } else {
  49. // Otherwise wait for construction, then send the notification
  50. mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
  51. public void onReactContextInitialized(ReactContext context) {
  52. LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
  53. }
  54. });
  55. if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
  56. // Construct it in the background
  57. mReactInstanceManager.createReactContextInBackground();
  58. }
  59. }
  60. }
  61. });
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement