Home Reference Source

js/MFPPush.js

/* Licensed Materials - Property of IBM
 * 5725-I43 (C) Copyright IBM Corp. 2018. All Rights Reserved.
 * US Government Users Restricted Rights - Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

/* author - Yash Soni | yashsoni21@in.ibm.com */

import {
    NativeModules
} from 'react-native';
import { _mandatoryParam, _checkParamClassType, _checkParamType } from './Utils'
import MFPSimplePushNotification from './MFPSimplePushNotification'
import { DeviceEventEmitter } from 'react-native';

DeviceEventEmitter.addListener('onNotificationReceived', function(e) {
    console.log(e);
    var notification = new MFPSimplePushNotification();
    notification.id = e.id;
    notification.url = e.url;
    notification.alert = e.alert;
    notification.payload = e.payload;
    DeviceEventEmitter.emit("appNotificationListener", notification);
});

var mfpPush = NativeModules.RNMFPPush;

/**
 * This class represents a singleton MFPPush.
 */
class MFPPush {
    /**
     * @ignore
     */
    constructor() {}

    /**
     * Initializes the MFPPush instance
     * @param {number} timeout - Integer value that specifies time out for the push requests made to the MFP server
     */
    initialize(timeout = -1) {
        _checkParamType(timeout, 'number');
        mfpPush.initialize(timeout);
    }

    /**
     * Retrieves all the subscriptions of the device
     * @return {Promise<string[], Error>}
     */
    async getSubscriptions() {
        return mfpPush.getSubscriptions();
    }

    /**
     * Retrieves all the available tags of the application
     * @returns {Promise<string[], Error>}
     */
    async getTags() {
        return mfpPush.getTags();
    }

    /**
     * Request MFPPush to stop delivering incoming push messages. After hold() is called, MFPPush will store the latest push message in private shared preference and deliver that message during the next MFPPush.listen().
     */
    hold() {
        mfpPush.hold();
    }

    /**
     * Request MFPPush to deliver incoming push messages.
     */
    listen() {
        mfpPush.listen();
    }

    /**
     * Registers the device with the push service.
     * @param {object} options - Android notification options.
     * @return {Promise<string, Error>} 
     */
    async registerDevice(options = {}) {
        _checkParamClassType(options, Object);
        return mfpPush.registerDevice(options);
    }

    /**
     * Unregisters the device from the push service.
     * @return {Promise<string, Error>}
     */
    async unregisterDevice() {
        return mfpPush.unregisterDevice();
    }

    /**
     * Subscribes the device to the given tags
     * @param {Array} tags - An array of string 'tags'
     * @return {Promise<string[], Error>}
     */
    async subscribe(tags = _mandatoryParam('tags')) {
        _checkParamClassType(tags, Array);
        return mfpPush.subscribe(tags);
    }

    /**
     * Unsubscribes the device from the given tags
     * @param {Array} tags - An array of string 'tags'
     * @return {Promise<string[], Error>}
     */
    async unsubscribe(tags = _mandatoryParam('tags')) {
        _checkParamClassType(tags, Array);
        return mfpPush.unsubscribe(tags);
    }
}

export default MFPPush