Cordova Plugin to allow message exchange between javascript and native (and viceversa).
Broadcaster plugin providing bridge for the following native technologies:
target OS | Native Technology |
---|---|
IOS | NotificationCenter |
Android | LocalBroadcastManager |
date | infos | refs |
---|---|---|
Jan 16, 2018 | I've developed a complete ionic3 sample project using broadcaster | ionic-broadcaster-sample |
Jan 28, 2017 | such plugin has been added to ionic-native distribution | How to is available here |
$ cordova create <PATH> [ID [NAME [CONFIG]]] [options]
$ cd <PATH>
$ cordova platform add [ios|android]
$ cordova plugin add cordova-plugin-broadcaster
$ cordova plugin add https://github.com/wlnjtan/cordova-plugin-broadcaster.git
console.log( "register didShow received!" );
var listener = function( e ) {
//log: didShow received! userInfo: {"data":"test"}
console.log( "didShow received! userInfo: " + JSON.stringify(e) );
}
window.broadcaster.addEventListener( "didShow", listener);
final Intent intent = new Intent("didShow");
Bundle b = new Bundle();
b.putString( "data", "test" );
intent.putExtras( b);
LocalBroadcastManager.getInstance(this).sendBroadcastSync(intent);
[[NSNotificationCenter defaultCenter] postNotificationName:@"didShow"
object:nil
userInfo:@{ @"data":@"test"}];
let nc = NSNotificationCenter.default
nc.post(name:"didShow", object: nil, userInfo: ["data":"test"])
let event = new CustomEvent("didShow", { detail: { data:"test"} } );
document.dispatchEvent( event )
window.broadcaster.fireNativeEvent( "test.event", { item:'test data' }, function() {
console.log( "event fired!" );
} );
final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String data = intent.getExtras().getString("data");
Log.d("CDVBroadcaster",
String.format("Native event [%s] received with data [%s]", intent.getAction(), data));
}
};
LocalBroadcastManager.getInstance(this)
.registerReceiver(receiver, new IntentFilter("test.event"));
}
[[NSNotificationCenter defaultCenter] addObserverForName:@"test.event"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *notification) {
NSLog(@"Handled 'test.event' [%@]", notification.userInfo[@"item"]);
}];
let nc = NotificationCenter.default
nc.addObserver(forName:Notification.Name(rawValue:"test.event"),
object:nil, queue:nil) {
notification in
print( "\(notification.userInfo)")
}
document.addEventListener( "test.event", ( ev:Event ) => {
console.log( "test event", ev.detail );
});