Reputation: 3322
I am trying to send an event from javascript (angular to be specific) to android via the broadcaster plugin : https://github.com/bsorrentino/cordova-broadcaster
In the UI i fire a native event with some data:
this.broadCaster
.fireNativeEvent("com.service.print", { extras: { item: "test data" } })
.then((result) => {
console.log("broadcast sent:" + result);
})
.catch((error) => {
console.log("Error sending broadcast:" + error);
});
On the android i registered a receiver in AndroidManifest.xml
:
<receiver android:name="io.ionic.starter.PrintReceiver">
<intent-filter>
<action android:name="com.service.print"></action>
</intent-filter>
</receiver>
And added the PrintReceiver
class to handle the broadcast
:
public class PrintReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String data = intent.getExtras().getString("data");
List<PrinterCommand> commands = new ArrayList<PrinterCommand>();
// Add commands to be sent
commands.add(
new PrinterCommand(PrinterCommand.CommandType.TEXT, "Normal row 1\n")
);
Gson gson = new Gson();
String json = gson.toJson(commands);
System.out.println("Sending print broadcast: " + json);
Intent printIntent = new Intent(MyPOSUtil.PRINT_BROADCAST);
// Add the commands
printIntent.putExtra("commands", json);
// Send broadcast
LocalBroadcastManager.getInstance(context).sendBroadcastSync(printIntent);
}
}
When i am trying to run the app on Android device i get this error:
2020-07-31 10:31:34.007 11308-11416/io.ionic.starter V/Capacitor/Plugin: To native (Cordova plugin): callbackId: broadcaster343151718, service: broadcaster, action: fireNativeEvent, actionArgs: ["com.service.print",{"extras":{"item":"test data"}},false]
2020-07-31 10:31:34.022 11308-11433/io.ionic.starter V/CDVBroadcaster: sendBroadcast isGlobal=false
2020-07-31 10:31:34.051 11308-11308/io.ionic.starter I/Capacitor/Console: File: http://localhost/auth-auth-module-es2015.js - Line 70 - Msg: Error sending broadcast:OK
Note: i tried using the same plugin to receive a broadcast from android and it works.
What could be the issue here.
Upvotes: 0
Views: 1572
Reputation: 3322
Ok Eventually i figured out that there was nothing wrong with the brodcaster plugin and that it actually sends a correct broadcast.
What i learnt is that as of Android 8 you cannot register your receiver in the Manifest.xml, but use context based registration.
Android 8+ manifest registration restrictions
Upvotes: 1