I'm using ionic-react and capacitor. I need to trigger an event from the Android native code and handle it from JavaScript. Here is my test code:
In MainActivity.java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initializes the Bridge
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
// Additional plugins you've installed go here
// Ex: add(TotallyAwesomePlugin.class);
}});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
bridge.triggerWindowJSEvent("MyPushEvent", "test");
}
}, 25000);
}
In App.tsx
window.addEventListener("MyPushEvent", function (msg) {
console.log("Push Event: " + msg);
alert("Window Push Event: " + msg);
});
Basically, I'm calling bridge.triggerWindowJSEvent in onCreate after a long delay, making sure the event handler on the client has been added. However, nothing happens on the client side.
Please help!