Git Product home page Git Product logo

aerogear-reactnative-push's Introduction

@aerogear/aerogear-reactnative-push

Getting started

$ npm install @aerogear/aerogear-reactnative-push --save

Native Project Configuration

The ReactNative Unified Push Client requires configuration of the native projects to consume push messages. Please refer to your platform's documentation or the Unified Push documentation for details.

Usage Examples

There are two parts to using the library inside of your application. Your application must register itself with UnifiedPush and then your application will be able to handle messages.

Registration

The client SDK must be provided with the variant information and, in the case of Android, the senderID. These values may be provided together as in the example below, or you may wish to use platform specific code. Once you have provided these values, the RNUnifiedPush instance is ready to register your application with UnifiedPush.

You will want to run this process at the boot of your application, and if your user changes their alias or subscribed categories.

import RNUnifiedPush from '@aerogear/aerogear-reactnative-push';

const ups = new RNUnifiedPush();

ups.init(//Initialization provides the UPS address and variant secrets
{ 
pushServerURL: "http://10.1.10.51:9999/",
ios: {
    variantID: "91c039f9-d657-49cd-b507-cb78bea786e3",
    variantSecret: "4b7fd0b4-58b5-46e8-80ef-08a6b8d449cd"
}, 
android: {
    senderID: "557802659713",
    variantID: "c046f7b6-cf86-4ae6-9eb0-bb39104ea38b",
    variantSecret: "4907e063-cd39-477f-8938-2d817eabff97"
}
}).then(//Registration connects your device to UnifiedPush
() => {
    return ups.register({
    alias:"rnAlias",
    categories:["cat1", "cat2"]
    })
}).then(()=> this.onRegister && this.onRegister();  //You are now ready to receive push messages, inform your app
).catch(//Always catch your errors
(err) => console.log("Err", err));

Handling Messages

RNUnifiedPush instances receive messages from the native platform the application is running on. The library passes these messages largely as is and the developer is responsible for ensuring that their application handles both iOS and Android messages. Do this by checking for the aps property of the message object, or by platform specific code. The following example uses different code files for Android and iOS to define our handler, and then it registers to a UnifiedPush server using our RNUnifiedPush instance.

handler.android.js

//Extract the message text and add it to a state variable in a React application
export default function(message) { this.setState({messages: [...this.state.messages, message.alert] })}

handler.ios.js

//Extract the message text and add it to a state variable in a React application
export default function(message) {this.setState({messages: [...this.state.messages, message.aps.alert.body] })};

app.js

    import handler from './handler';
    /*
        SNIPPING imports and registration
    */
    ups.registerMessageHandler(handler.bind(this));  //.bind(this) allows `this` to work in the above examples

Cookbook application

There is a full example of this application in our cookbook repository

aerogear-reactnative-push's People

Contributors

secondsun avatar ziccardi avatar

Watchers

 avatar

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.