diff --git a/src/Adapters/Push/ParsePushAdapter.js b/src/Adapters/Push/ParsePushAdapter.js index 3f554054ed..c695dbee50 100644 --- a/src/Adapters/Push/ParsePushAdapter.js +++ b/src/Adapters/Push/ParsePushAdapter.js @@ -4,7 +4,7 @@ // for ios push. const Parse = require('parse/node').Parse; -const GCM = require('../../GCM'); +const GCM = require('../../GCM').GCM; const APNS = require('../../APNS'); import PushAdapter from './PushAdapter'; import { classifyInstallations } from './PushAdapterUtils'; diff --git a/src/GCM.js b/src/GCM.js index a13a67518e..e52525f570 100644 --- a/src/GCM.js +++ b/src/GCM.js @@ -7,7 +7,7 @@ const cryptoUtils = require('./cryptoUtils'); const GCMTimeToLiveMax = 4 * 7 * 24 * 60 * 60; // GCM allows a max of 4 weeks const GCMRegistrationTokensMax = 1000; -function GCM(args) { +export function GCM(args) { if (typeof args !== 'object' || !args.apiKey) { throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, 'GCM Configuration is invalid'); @@ -22,16 +22,8 @@ function GCM(args) { * @returns {Object} A promise which is resolved after we get results from gcm */ GCM.prototype.send = function(data, devices) { - let pushId = cryptoUtils.newObjectId(); - let timeStamp = Date.now(); - let expirationTime; - // We handle the expiration_time convertion in push.js, so expiration_time is a valid date - // in Unix epoch time in milliseconds here - if (data['expiration_time']) { - expirationTime = data['expiration_time']; - } // Generate gcm payload - let gcmPayload = generateGCMPayload(data.data, pushId, timeStamp, expirationTime); + let gcmPayload = generateGCMPayload(data); // Make and send gcm request let message = new gcm.Message(gcmPayload); @@ -68,7 +60,17 @@ GCM.prototype.send = function(data, devices) { * @param {Number|undefined} expirationTime A number whose format is the Unix Epoch or undefined * @returns {Object} A promise which is resolved after we get results from gcm */ -function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) { +export function generateGCMPayload(data) { + let pushId = cryptoUtils.newObjectId(); + let timeStamp = Date.now() + let coreData = data.data; + let expirationTime; + // We handle the expiration_time convertion in push.js, so expiration_time is a valid date + // in Unix epoch time in milliseconds here + if (data['expiration_time']) { + expirationTime = data['expiration_time']; + } + let payloadData = { 'time': new Date(timeStamp).toISOString(), 'push_id': pushId, @@ -78,6 +80,7 @@ function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) { priority: 'normal', data: payloadData }; + if (expirationTime) { // The timeStamp and expiration is in milliseconds but gcm requires second let timeToLive = Math.floor((expirationTime - timeStamp) / 1000); @@ -110,4 +113,3 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') { GCM.generateGCMPayload = generateGCMPayload; GCM.sliceDevices = sliceDevices; } -module.exports = GCM;