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
}
}
}
}
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);
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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With