2.2.12 • Published 7 years ago

cordova-plugin-broadcaster-ii v2.2.12

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Cordova Broadcaster

Cordova Plugin to allow message exchange between javascript and native (and viceversa).

npm Join the chat at https://gitter.im/bsorrentino/cordova-broadcaster

Ingredient Technologies

Broadcaster plugin providing bridge for the following native technologies:

target OSNative Technology
IOSNotificationCenter
AndroidLocalBroadcastManager

News

Jan 28, 2017such plugin has been added to ionic-native distributionHow to is available here

Installation

$ cordova create <PATH> [ID [NAME [CONFIG]]] [options]
$ cd <PATH>
$ cordova platform add [ios|android]
$ cordova plugin add cordova-plugin-broadcaster

Usage:

From Native to Javascript

Javascript

    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);

IOS

Objective-C

[[NSNotificationCenter defaultCenter] postNotificationName:@"didShow"
                                                    object:nil
                                                  userInfo:@{ @"data":@"test"}];

Swift 2.2

let nc = NSNotificationCenter.defaultCenter()
nc.postNotificationName("didShow",
                        object: nil,
                        userInfo: ["data":"test"])

Swift 3.0

let nc = NSNotificationCenter.default
nc.post(name:"didShow", object: nil, userInfo: ["data":"test"])

ANDROID

final Intent intent = new Intent("didShow");

Bundle b = new Bundle();
b.putString( "userdata", "{ data: 'test'}" );
intent.putExtras( b);

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

From Javascript to Native

Javascript

  window.broadcaster.fireNativeEvent( "test.event", { item:'test data' }, function() {
    console.log( "event fired!" );
    } );

IOS

Objective-C

[[NSNotificationCenter defaultCenter] addObserverForName:@"test.event"
                                                  object:nil
                                                   queue:[NSOperationQueue mainQueue]
                                              usingBlock:^(NSNotification *notification) {
                                                      NSLog(@"Handled 'test.event' [%@]", notification.userInfo[@"item"]);
                                                    }];

Swift 2.2

let nc = NSNotificationCenter.defaultCenter()
nc.addObserverForName("test.event",
               object: nil,
               queue:nil ) {
  notification in
    print( "\(notification.userInfo)")
}

Swift 3.0

let nc = NotificationCenter.default
nc.addObserver(forName:Notification.Name(rawValue:"test.event"),
               object:nil, queue:nil) {
  notification in
  print( "\(notification.userInfo)")
}

ANDROID

final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            final JSONObject data = new JSONObject( intent.getExtras().getString("userdata"));

            Log.d("CDVBroadcaster",
                    String.format("Native event [%s] received with data [%s]", intent.getAction(), String.valueOf(data)));

        } catch (JSONException e) {
           throw new RuntimeException(e);
        }
    }
};

LocalBroadcastManager.getInstance(this)
            .registerReceiver(receiver, new IntentFilter("test.event"));
}
2.2.12

7 years ago

2.2.11

7 years ago

2.2.10

7 years ago

2.2.9

7 years ago

2.2.8

7 years ago

2.2.7

7 years ago

2.2.6

7 years ago

2.2.5

7 years ago

2.2.4

7 years ago

2.2.3

7 years ago

2.2.2

7 years ago

2.2.1

7 years ago