onNewIntent() is not called on ReactContextBaseJav

2020-03-30 02:38发布

问题:

I'm building a react native module, from my module I send a PendingIntent like this.

Intent postAuthorizationIntent = new Intent("com.example.HANDLE_AUTHORIZATION_RESPONSE");
PendingIntent pendingIntent = PendingIntent.getActivity(reactContext.getApplicationContext(), request.hashCode(), postAuthorizationIntent, 0);

If I modify the MainActivity then I can receive data inside onNewIntent() method. The class looks like this...

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "example";
    }

    @Override
    public void onNewIntent(Intent intent) {
        checkIntent(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        checkIntent(getIntent());
    }

    private void checkIntent(@Nullable Intent intent) {
        if (intent != null) {
            String action = intent.getAction();
            switch (action) {
                case "com.example.HANDLE_AUTHORIZATION_RESPONSE":
                    ...
                    break;
                default:
                    // do nothing
            }
        }
    }
}

Then when I try to move this logic to my Module, nothing happens, it does not work.

public class ExampleModule extends ReactContextBaseJavaModule implements ActivityEventListener{

    private ReactApplicationContext reactContext;
    private String action = "";

    public ExampleModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
        this.action = "com.example.HANDLE_AUTHORIZATION_RESPONSE";
        reactContext.addActivityEventListener(this);
      }

     @Override
    public String getName() {
        return "ExampleModule";
      }

     @Override
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {

      }

      @Override
      public void onNewIntent(Intent intent) {
        checkIntent(intent);
      }

      private void checkIntent(@Nullable Intent intent) {
        if (intent != null) {
            String action = intent.getAction();
            switch (action) {
                case "com.example.HANDLE_AUTHORIZATION_RESPONSE":
                    ...
                    break;
                default:
                    // do nothing
            }
        }
    }
}

回答1:

I had this same issue and fixed it by calling super.onNewIntent(intent) from MainActivity:

@Override
public void onNewIntent(Intent intent) {
    setIntent(intent);
    super.onNewIntent(intent);
}

With this in place, onNewIntent is called in your module - assuming your module implements ActivityEventListener and you've registered it as a listener in the constructor:

reactContext.addActivityEventListener(this);


回答2:

Ok I solved the issue.

public class RNSsfAuthModule extends ReactContextBaseJavaModule implements ActivityEventListener, Application.ActivityLifecycleCallbacks{
    ....

    public ExampleModule(ReactApplicationContext reactContext, Application application) {
            super(reactContext);
            ...
            reactContext.addActivityEventListener(this);
            application.registerActivityLifecycleCallbacks(this);
          }

    ...


    @Override
    public void onActivityStarted(Activity activity) {
        checkIntent(activity.getIntent());
    }

}