Giter Site home page Giter Site logo

applicaster-plugins / react-native-broadcast-manager Goto Github PK

View Code? Open in Web Editor NEW
3.0 12.0 1.0 123 KB

Broadcast manager native module. This module utilizes an Android broadcast-receiver and iOS Notification center to send a broadcast message from the react-native side.

License: MIT License

Kotlin 31.41% JavaScript 3.40% Objective-C 9.07% Ruby 56.12%
react-native native-module broadcast-reciever notification-center

react-native-broadcast-manager's Introduction

react-native-broadcast-manager

A bridge for sending broadcast events from react-native to native code

Author: Applicaster LTD

Platform Support: iOS, tvOS, Android, AndroidTV

Usage (RN Side)

Call sendBroadcastEvent with an event_key (String) and even_properties (type JSON) that later can be caught in the native code and handled upon receiving.

  1. Add the dependency:

    yarn add @applicaster/react-native-broadcast-manager

    or

    npm install @applicaster/react-native-broadcast-manager

  2. Send a broadcast message

    import { sendBroadcastEvent } from '@applicaster/react-native-broadcast-manager';
    
    // ...
    
    sendBroadcastEvent("someEventName", {
      "key_1" : "value_1",
      "key_2" : "value_2",
      //...
      "key_n" : "value_n"
    });

Usage (iOS)

Catch the event in your native project by adding the following code:

  private let SomeEventName = Notification.Name("someEventName")

  //..

  //Listen to react native broadcase events,
  //Don't forget to release it on deinit!
  NotificationCenter.default.addObserver(self,
                selector: #selector(didReceiveEvent(_:)),
                name: SomeEventName,
                object: nil)

  //..

  @objc func didReceiveEvent(_ notification:Notification) {
    if let userInfo = notification.userInfo as [String: Any] {
      /*
      The userInfo will contain a dictionary with the JSONObject you sent in the React-Native side.
      */

      //Add more logic here.
    }
  }

Usage (Android)

Catch the event in your native project by adding the following code:

    public static final String EVENT_NAME = "event_name";
    public static final String EVENT_PROPERTIES = "event_properties";
    //..

    public static final String SEND_BROADCAST_ACTION = "send_broadcast_from_rn";

    // Catch the send_broadcast_from_rn action sent from the RN Broadcast Manager
    LocalBroadcastManager.getInstance(context).registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
                try {
                  //Filter for the intent you sent in the r.n side
                  if ("someEventName".equals(intent.getStringExtra(EVENT_NAME))) {
                    //Get the event properties
                    val event_properties = new JSONObject(intent.getStringExtra(EVENT_PROPERTIES));
                    //Add more logic here
                  }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

        }
    }, new IntentFilter(SEND_BROADCAST_ACTION));

Manifest file setup (for Applicaster plugins only)

If you working with an Applicaster plugin then add the following configuration to your manifest file.

  • Android / Android TV
"api": {
  //..
  “class_name”: “com.applicaster.reactnative.plugins.APReactNativeAdapter”,
  "react_packages": [
    //..
    //Add the following to the bottom of the list.
    "com.applicaster.react.BroadcastManagerAPIPackage"
  ]
},
/* ... */
"npm_dependencies": [
  "@applicaster/react-native-broadcast-manager"
],
/* ... */
"project_dependencies": [
  {
    "react-native-broadcast-manager": "./node_modules/@applicaster/react-native-broadcast-manager/Android"
  }
],
/* ... */
  • iOS / tvOS
/* ... */
"extra_dependencies": [
  {
    "react-native-broadcast-manager": ":path => './node_modules/@applicaster/react-native-broadcast-manager'"
  }
],
/* ... */
"npm_dependencies": [
  "@applicaster/react-native-broadcast-manager"
],
/* ... */

License

MIT

react-native-broadcast-manager's People

Contributors

shaymark avatar snooky23 avatar szhigunov avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

elizasapir

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.