react-native-ibm-mobilefirst-push
Version:
React Native SDK for IBM Mobile Push. Works with IBM Mobile First.
129 lines (114 loc) • 4.2 kB
JavaScript
/* 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,
DeviceEventEmitter,
NativeEventEmitter
} from 'react-native';
import { _mandatoryParam, _checkParamClassType, _checkParamType } from './Utils'
import MFPSimplePushNotification from './MFPSimplePushNotification'
const eventEmitter = new NativeEventEmitter(NativeModules.RNMFPPush)
var mfpPush = NativeModules.RNMFPPush;
const emitterMap = new Map();
/**
* This class represents a singleton MFPPush. It provides the apis to use the Push Service
*/
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();
}
/**
* Registers a Notification Callback - it is an Event Listener where you will receive event for
* notifications sent from MFP Server.
* @param {string} eventListenerName - Event Listener name. You will implement an Event Listener
* with this name in your App to be notified when a notification is received. This is like a callback
* for notification received event.
*/
registerNotificationsCallback(eventListenerName = _mandatoryParam('eventListenerName')) {
_checkParamType(eventListenerName, 'string');
if(!emitterMap.has(eventListenerName)) {
emitterMap.forEach(function(value, key, map) {
// value is a EmitterSubscription Object
// 'remove' is actually removing the subscription
value.remove();
})
emitterMap.set(
// key -- string
eventListenerName,
//value -- EmitterSubscription Object
eventEmitter.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;
eventEmitter.emit(eventListenerName, notification);
})
);
mfpPush.registerNotificationsCallback();
}
}
/**
* 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