bsorrentino / cordova-broadcaster

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

addEventListner does not get called back #17

Closed yannolaf closed 7 years ago

yannolaf commented 7 years ago

I am trying to use this plugin to receive android broadcast intents. I register for the broadcast event

      window.broadcaster.addEventListener("com.android.action.SEND_SCAN_RESULT", function (e) { 
             console.log("com.android.action.SEND_SCAN_RESULT received."); 
        });
       console.log("com.android.action.SEND_SCAN_RESULT registered");

but if the intent gets fired

    V/ActivityManager(  775): Broadcast: Intent { act=com.android.action.SEND_SCAN_RESULT flg=0x10 (has extras) } ordered=false userid=0 callerApp=ProcessRecord{1418277c 775:system/1000}

the callback does not get triggered.

The only log message is

com.android.action.SEND_SCAN_RESULT registered

do I have to add anything to the Android Manifest file?

bsorrentino commented 7 years ago

how to send message from native code?

LocalBroadcastManager.getInstance(this).sendBroadcastSync(intent);

Keep in mind that the plugin registers the handler using

LocalBroadcastManager.getInstance(super.webView.getContext()).registerReceiver(receiver,filter);
yannolaf commented 7 years ago

The broadcast is send from an other application. Is received by my application see ActivityManager log.

yannolaf commented 7 years ago

Is there a working sample application where I can take a look?

bsorrentino commented 7 years ago

Currently the plugin doesn't manage external broadcast events but only the local ones

Probably solution could be handle the external event in android and then fire it up to javascript

take a look here

As soon as possible I'll provide you an example on that

bsorrentino commented 7 years ago

Try this and let me know

manifest


 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name="MyReceiver" >
            <intent-filter>
                <action android:name="com.android.action.SEND_SCAN_RESULT" >
                    </action>
            </intent-filter>
        </receiver>
    </application>

Receiver

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
                 LocalBroadcastManager.getInstance(context).sendBroadcastSync(intent);
    }
}
yannolaf commented 7 years ago

Great! Thanks for the solution :)