• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    Java
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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 OS Native Technology
IOS NotificationCenter
Android LocalBroadcastManager

News

date infos refs
Mar 19, 2020 Concerning Android I've added support for broadcast Intent to external Apps, receive broadcast Intents from external Apps, Flags & Category on Intent insipred by navarrojava's fork
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

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

From ANDROID to Javascript - Extra ANDROID features

Listen for global message

if( cordova.platformId === "android" ) {

    var listener = function( e ) {
      //log: didShow received! userInfo: {"data":"test"}
      console.log( "CONNECTIVITY_CHANGE: " + JSON.stringify(e)  );
    }
    var isGlobal = true
    window.broadcaster.addEventListener( 'android.net.conn.CONNECTIVITY_CHANGE', isGlobal, listener);
}

ANDROID

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

final Bundle child = new Bundle();
child.putString( "name", "joker");

final Bundle b = new Bundle();
b.putString( "data", "test");
b.putBoolean( "valid", true );
b.putBundle( "child", child );

intent.putExtras( b);

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

IOS

Objective-C
    NSDictionary * payload = @{
        @"data":@"test",
        @"valid": [NSNumber numberWithBool:YES],
        @"child": @{ @"name": @"joker" }
    };

    [[NSNotificationCenter defaultCenter] postNotificationName:@"TEST.EVENT"
                                                        object:nil
                                                      userInfo:payload];
Swift 5.x
  let payload:[String:Any] = [
          "data":"test",
          "valid": true,
          "child":[ "name": "joker" ]
      ]

  let nc = NotificationCenter.default
  nc.post(name:Notification.Name("didShow"), object: nil, userInfo: payload)

BROWSER

let event = new CustomEvent("didShow", { detail: { data:"test"} } );
document.dispatchEvent( event )

From Javascript to Native - ANDROID,BROWSER,IOS

Javascript

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

From Javascript to ANDROID - Extra ANDROID features

Send a message with "flags" and "category"

if( cordova.platformId === "android" ) {

  // send a message with "flags" and "category"
  window.broadcaster.fireNativeEvent( "message", { extras:{ item:'test data' }, flags:0, category:'android.intent.category.INFO', packageName:'org.bsc'}, function() {
    console.log( "event fired!" );
  });
}

Send a global message

if( cordova.platformId === "android" ) {

  // send a global message
  var isGlobal = true
  window.broadcaster.fireNativeEvent( "GLOBAL_ACTION", isGlobal, { item:'test data' }, function() {
    console.log( "event fired!" );
  });

}

Send a global message with "flags" and "category"

if( cordova.platformId === "android" ) {

  // send a global message with "flags" and "category"
  var isGlobal = true
  window.broadcaster.fireNativeEvent( "GLOBAL_ACTION", isGlobal, { extras:{ item:'test data' }, flags:0, category:'android.intent.category.INFO', packageName:'org.bsc'}, function() {
    console.log( "event fired!" );
  });

}

ANDROID

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

IOS

Objective-C
[[NSNotificationCenter defaultCenter] addObserverForName:@"test.event"
                                                  object:nil
                                                   queue:[NSOperationQueue mainQueue]
                                              usingBlock:^(NSNotification *notification) {
                                                      NSLog(@"Handled 'test.event' [%@]", notification.userInfo[@"item"]);
                                                    }];
Swift 5.x
let nc = NotificationCenter.default
nc.addObserver(forName:Notification.Name(rawValue:"test.event"),
               object:nil, queue:nil) {
  notification in
  print( "\(notification.userInfo)")
}

BROWSER

document.addEventListener( "test.event", ( ev:Event ) => {
  console.log( "test event", ev.detail );
});

More Repositories

1

maven-confluence-plugin

Maven plugin that generates project's documentation directly to confluence allowing to keep in-sync project evolution with its documentation
Java
60
star
2

swiftui-fieldvalidator

SwiftUI Package supporting "Form Validation"
Swift
50
star
3

maven-annotation-plugin

A maven plugin to process annotation from jdk6 and above at compile time
HTML
29
star
4

java2typescript

Java Processor to generate Typescript Definition from Java classes - This is to help developing on JVM javascript engine (ie Nashorn) using Typescript
Java
26
star
5

pdf-tools

Extract Markdown + Images from PDF
TypeScript
16
star
6

cordova-poc

Mobile application to provides Cordova Proof Of Concept (POC)
8
star
7

ionic-broadcaster-sample

Sample usage of cordova-broadcaster in ionic2 using ionic-native
TypeScript
8
star
8

confluence-site-publisher

CLI for publish directly to confluence allowing to keep in-sync local and remote documentation
TypeScript
8
star
9

keychain-ios-app

IOS App that allows to manage your Login/password catalog through native keychain
Swift
5
star
10

WSDLDownloader

Utility to download WSDL/XSD including transitive dependencies (eg. xs:include)
Java
5
star
11

Excel-Xml-Mapping

Apache POI extension to support xml mapping in excel 2007
Java
5
star
12

PlantUML4iPad

PlantUML Editor App for iPad
Swift
4
star
13

slidesOnTV

Present slides on TV - for Apple tvOS
Swift
4
star
14

rxjs-samples

Example related to a Meetup at #DevDay concerning - Reactive Programming and the RX (r)evolution
TypeScript
4
star
15

OfficeScripts-CLI

CLI for manage OSTS files
TypeScript
3
star
16

istio-sample

project to evaluate istio (service mesh) functionalities
HTML
3
star
17

generator-polymerts

Scaffold out a PolymerTS's element
TypeScript
3
star
18

maven-bulk-deploy

Plugin that makes easy implementing BOM approach. It allows to install & deploy libraries from folder(s) generating BOM file.
Java
3
star
19

bsorrentino

My personal repository
2
star
20

minitemplator

MiniTemplator is a compact template engine for HTML files. (fork of http://www.source-code.biz/MiniTemplator/)
Java
2
star
21

BSCButtonBar

A button bar with animated indicator, useful used as sortable header for UITableView
Objective-C
1
star
22

node-reactive-system

Experiment on developing a reactive system in nodejs
TypeScript
1
star
23

SwiftUI-SearchBar

A SearchBar for SwiftUI reusing UIKIT component
Swift
1
star
24

ace-gwt

using ACE editor from Google Web Toolkit
JavaScript
1
star
25

android-js

Enable Android's native programming using Javascript
Java
1
star
26

UIXML

Library to generate UITableView based form(s) form from .plist file(s)
Objective-C
1
star
27

mac-cleaner

A simple macosx utility to completely remove apps & software
TypeScript
1
star
28

zx-powerapps-cli

zx scripts for interactive use of powerapps cli (PAC).
JavaScript
1
star
29

java2typescript-demo

Demo for project java2typescript - https://github.com/bsorrentino/java2typescript
TypeScript
1
star
30

ts-isometric-map

Isometric map made with Typescript and HTML5 Canvas
TypeScript
1
star
31

robovm-jsfiddle-javafx-app

My first robovm app using javafx for simple jsfiddle management
Java
1
star
32

maven-annotation-plugin.jaxrs-wiki-processor

JAVA ANNOTATION PROCESSOR TO GENERATE WIKI FROM JAX-RS ANNOTATION
Java
1
star
33

nstruts

The jakarta Struts framework porting on ASP.NET
C#
1
star