/**
* Copyright© 2016, Oracle and/or its affiliates. All rights reserved.
*/
//Must install phonegap-push, or any notification plugin to get deviceToken for that particular OS.
var NOTIFICATION_PROVIDERS = {
GCM: 'GCM',
FCM: 'FCM',
APNS: 'APNS'
};
var PLATFORM_ID = {
ANDROID: 'Android',
IOS: 'ios'
};
/**
* Class that provides notification capabilities. Callers should use
* MobileBackend's [Notifications()]{@link MobileBackend#Notifications} property.
* @constructor
* @global
*/
function Notifications(backend, utils, platform, logger) {
var _backend = backend;
var notifications = this;
/**
* Returns a string with device information used by [Notifications]{@link Notifications}
* @returns {String} The device specific information for platform.
* @example : "IOS,ANDROID"
*/
this.getDevicePlatform = function() {
var platform = 'null';
if(typeof device !== 'undefined'){
platform = device.platform;
} else if(navigator.userAgent.match(/Android/i)){
platform = PLATFORM_ID.ANDROID;
} else if(navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/iPod/i) ||
navigator.userAgent.match(/iPhone/i)){
platform = PLATFORM_ID.IOS;
}
return platform.toUpperCase();
};
/**
* Registers the current Cordova app running on the device for receiving push notifications.
* @param deviceToken {String} Platform-specific device token.
* @example deviceToken: "AxdkfjfDfkfkfkfjfFdjfjjf=", deviceToken is sent from device.
* @param appId {String} Platform-specific application reverse domain identifier.
* @example appId: "com.yourcompany.project"
* @param appVersion {String} Platform-specific application version..
* @example appVersion: "1.0.2"
* @param notificationProvider {String}
* @param [successCallback] {Notifications~successCallback} Optional callback invoked on success (deprecated).
* @param [errorCallback] {Notifications~errorCallback} Optional callback invoked on failure (deprecated).
* @return {Promise.<NetworkResponse|NetworkResponse>}
*
* @example <caption>Example usage of mcs.MobileBackend.getNotifications().registerForNotifications()</caption>
mcs.MobileBackend.getNotifications()
.registerForNotifications("YOUR_DEVICE_TOKEN", "com.oracle.mobile.cloud.OMCPushNotifications", "1.0.0")
.then(registerSuccess, registerError);
function registerSuccess(response){
console.log(response.statusCode);// returns statusCode for this function.
}
function registerError(response){
}
*/
this.registerForNotifications = function(deviceToken, appId, appVersion, notificationProvider, successCallback, errorCallback) {
var platformName = this.getDevicePlatform();
if(!(notificationProvider in NOTIFICATION_PROVIDERS)){
throw Error('No Notification Provider Type called ' + notificationProvider + '\n' +
'please use one of those types\n' +
NOTIFICATION_PROVIDERS.APNS + '\n' +
NOTIFICATION_PROVIDERS.FCM + '\n' +
NOTIFICATION_PROVIDERS.GCM);
}
if(notificationProvider === NOTIFICATION_PROVIDERS.APNS && platformName !== PLATFORM_ID.IOS){
throw Error('Notification Provider Type APNS can be used only on IOS platform');
}
if(platformName === PLATFORM_ID.IOS && notificationProvider !== NOTIFICATION_PROVIDERS.APNS){
throw Error('On IOS platform can be used only APNS Notification Provider Type');
}
var headers = _backend.getHttpHeaders(utils.MODULE_NAMES.NOTIFICATIONS);
headers[utils.HEADERS.CONTENT_TYPE] = utils.ACCEPT_TYPES.APPLICATION_JSON;
var payload = getPayload(deviceToken, appId, notificationProvider, appVersion);
return platform.invokeService({
method: utils.HTTP_METHODS.POST,
url: _backend.getPlatformUrl('devices/register'),
headers: headers,
data: JSON.stringify(payload)
}).then(invokeServiceSuccess,invokeServiceError);
function invokeServiceSuccess(response) {
logger.info( 'Device registered for push notifications. ' + response.statusCode);
if (successCallback) {
successCallback(response.statusCode);
}
return response;
}
function invokeServiceError(response) {
logger.error('Device registration for push notifications failed! ' + response.statusCode + ':' + JSON.stringify(response));
if(errorCallback) {
errorCallback(response.statusCode, response.data);
} else {
return Promise.reject(response);
}
}
};
/**
* Deregisters the current Cordova app running on the device for receiving push notifications.
* @param deviceToken {String} Platform-specific successCallback token.
* @example deviceToken: "AxdkfjfDfkfkfkfjfFdjfjjf=", deviceToken is sent from device.
* @param appId {String} Platform-specific application reverse domain identifier.
* @example appId: "com.yourcompany.project"
* @param [successCallback] {Notifications~successCallback} Optional callback invoked on success (deprecated use promises instead).
* @param [errorCallback] {Notifications~errorCallback} Optional callback invoked on failure (deprecated use promises instead).
* @return {Promise.<NetworkResponse|NetworkResponse>}
*
* @example <caption>Example usage of mcs.MobileBackend.getNotifications().deregisterForNotifications()</caption>
mcs.MobileBackend.getNotifications()
.deregisterForNotifications("YOUR_DEVICE_TOKEN", "com.oracle.mobile.cloud.OMCPushNotifications", "1.0.0")
.then(deregisterSuccess, deregisterError);
function deregisterSuccess(response){
console.log(response.statusCode);// returns statusCode for this function.
}
function deregisterError(response){
}
*/
this.deregisterForNotifications = function(deviceToken, appId, successCallback, errorCallback) {
var headers = _backend.getHttpHeaders(utils.MODULE_NAMES.NOTIFICATIONS);
headers[utils.HEADERS.CONTENT_TYPE] = utils.ACCEPT_TYPES.APPLICATION_JSON;
var payload = getPayload(deviceToken, appId);
return platform.invokeService({
method: utils.HTTP_METHODS.POST,
url: _backend.getPlatformUrl('devices/deregister'),
headers: headers,
data: JSON.stringify(payload)
}).then(invokeServiceSuccess,invokeServiceError);
function invokeServiceSuccess(response) {
logger.info('Device deregistered for push notifications succeeded. ' + response.statusCode);
if (successCallback) {
successCallback(response.statusCode);
}
return response;
}
function invokeServiceError(response) {
logger.error('Device deregistration for push notifications failed! ' + response.statusCode);
if(errorCallback) {
errorCallback(response.statusCode, response.data);
} else {
return Promise.reject(response);
}
}
};
function getPayload(deviceToken, appId, notificationProvider, appVersion){
var _platform = notifications.getDevicePlatform();
var payload = {
'notificationToken': deviceToken,
'mobileClient': {
'id': appId,
'platform': _platform
}
};
if(notificationProvider) {
payload.notificationProvider = notificationProvider;
}
if(appVersion) {
payload.mobileClient.version = appVersion;
}
return payload;
}
}