I have three Ionic 5 apps. If I open the first app and either of the other two apps is running in the background, I need to send some data, but only if the second app is present in the background.
I'm currently attempting to utilize the Cordova Broadcaster plugin (https://github.com/bsorrentino/cordova-broadcaster). While I've successfully managed to call the send function from the first app, I'm encountering an issue where the broadcast is not being received by the second app.
Sender App Code (It is called on click of a button):-
import { Broadcaster } from '@awesome-cordova-plugins/broadcaster/ngx';
constructor( private broadcaster :Broadcaster){}
sendFunction(){
let options1 = {"sample":"data"};
if (this.platform.is('android') && this.platform.is('capacitor')) {
var isGlobal = true
this.broadcaster.fireNativeEvent("SOR_APPS_TOKEN", isGlobal, options1).then(res => {
console.log("event fired!");
})
}
}
Receiver App Code :-
import { Broadcaster } from '@awesome-cordova-plugins/broadcaster/ngx';
constructor( private broadcaster :Broadcaster){}
onInit(){
if (this.platform.is('android') && this.platform.is('capacitor')) {
console.log( "register didShow received!" );
let listener = function( e ) {
console.log( "SOR_APPS_TOKEN received! userInfo: " + JSON.stringify(e) );
}
this.broadcaster.addEventListener( "SOR_APPS_TOKEN").subscribe( listener );
}
}
And in Manifest files I have added (for broadcast receiving apps) :-
<receiver android:name="SOR_APPS_TOKEN" >
<intent-filter>
<action android:name="com.example.app1" >
</action>
</intent-filter>
</receiver>
P.S. Any sample app on GitHub illustrating this functionality would be greatly appreciated.
UPDATE :-
Here is a link of other option that i have tried out (which also dosent work) :- https://github.com/bsorrentino/cordova-broadcaster/issues/72
I was able to resolve the issue by using Ionic webIntent (https://www.npmjs.com/package/@ionic-native/web-intent)
From App 1 (which is in ForeGround):-
try {
let extraName="TEST_DATA";
this.webIntent.sendBroadcast({
action: 'com.test.app1.APPS_BROADCAST',
extras: {
[extraName]: 'TEST_DATA_3',
"SEND_RESULT": 'TEST_DATA_4'
}
});
} catch (e) {
console.log("Error in sending broadcast ", e);
}
And in App 2 (that is in background) register the receiver as :-
try{
this.webIntent.registerBroadcastReceiver({
filterActions: [
'com.test.app1.APPS_BROADCAST
],
filterCategories: [
'android.intent.category.DEFAULT'
]
}).subscribe((intent) => {
console.log('================== this.webIntent.registerBroadcastReceiverReceived Intent: ' + JSON.stringify(intent));
});
}catch(e){
console.log("========= this.webIntent.registerBroadcastReceiver Error in registering broadcast receiver",e);
}