bsorrentino / cordova-broadcaster

Cordova Plugin to allow message exchange between javascript and native (and viceversa)
MIT License
113 stars 53 forks source link

Ionic 5 + Capacitor - How to implement external broadcaster ? #72

Open givemesumthing opened 1 month ago

givemesumthing commented 1 month ago

How to implement external broadcaster , i.e. send data from one app to another ?

I have tried :-

From App 1 :-

        let options1 = {
          filterActions: ['com.example.APPS_TOKEN'],
          extras: {
            'param1': 'abc',
            'param2': 'xyz'
          }
        };
      if (this.platform.is('android') && this.platform.is('capacitor')) {
        var isGlobal = true
        this.broadcaster.fireNativeEvent("APPS_TOKEN", isGlobal, options1).then(res => {
          console.log("event fired!");
        })

And in App 2 :-

 let listener = function( e ) {
      console.log( "APPS_TOKEN received! userInfo: " + JSON.stringify(e)  );
    }

    this.broadcaster.addEventListener( "APPS_TOKEN").subscribe( listener );

But here data is not being sent to second app .

Is there a sample or documentation for it ? Also, do we need to set up anything in AndroidManifest.xml file ?

bsorrentino commented 1 month ago

Hi @givemesumthing

This plugin has been through to work for in-process-communication, that is, in a single app. Obviously it can be useful also in your case but it must be used on top of an inter-process-communication(IPC) protocol.

here is an article that could help you

givemesumthing commented 3 weeks ago

Hi @bsorrentino, For my specific requirement, I only need to broadcast to an app that remains open in the background, without necessitating its foreground activation. While the provided code enables broadcasting from one foreground app, I'm encountering difficulty in receiving it within app 2 using this plugin. Although I successfully registered the receiver in MainActivity, how can I effectively relay this information from the Android app's main activity to the plugin's this.broadcaster.addEventListener ?

bsorrentino commented 3 weeks ago

Hi @givemesumthing

I assume that you have already solved issue concerning inter-process-communication and you have tested that the message from external broker arrive correctly in your background app

Anyway I never tested the plugin in such condition. As you said, the plugin has been developed to be used in foreground app.

I haven't idea what happens in Cordova/Capacitor runtime when app goes in background, so we need to understand first if, in background mode, messages are correctly delivered and after that, investigate specifically on your use case