diff --git a/spec/APNS.spec.js b/spec/APNS.spec.js index c56e35d550..30fc4fc4f6 100644 --- a/spec/APNS.spec.js +++ b/spec/APNS.spec.js @@ -1,4 +1,4 @@ -var APNS = require('../src/APNS'); +var APNS = require('../src/APNS').APNS; describe('APNS', () => { diff --git a/spec/GCM.spec.js b/spec/GCM.spec.js index 0ddfe27fa0..43d3fb36e2 100644 --- a/spec/GCM.spec.js +++ b/spec/GCM.spec.js @@ -1,4 +1,4 @@ -var GCM = require('../src/GCM'); +var GCM = require('../src/GCM').GCM; describe('GCM', () => { it('can initialize', (done) => { diff --git a/spec/ParsePushAdapter.spec.js b/spec/ParsePushAdapter.spec.js index e21a9dbb21..0f29305e5d 100644 --- a/spec/ParsePushAdapter.spec.js +++ b/spec/ParsePushAdapter.spec.js @@ -1,6 +1,6 @@ var ParsePushAdapter = require('../src/Adapters/Push/ParsePushAdapter'); -var APNS = require('../src/APNS'); -var GCM = require('../src/GCM'); +var APNS = require('../src/APNS').APNS; +var GCM = require('../src/GCM').GCM; describe('ParsePushAdapter', () => { it('can be initialized', (done) => { diff --git a/src/APNS.js b/src/APNS.js index 69389ce8f7..f9e633ee73 100644 --- a/src/APNS.js +++ b/src/APNS.js @@ -16,7 +16,7 @@ const apn = require('apn'); * @param {String} args.bundleId The bundleId for cert * @param {Boolean} args.production Specifies which environment to connect to: Production (if true) or Sandbox */ -function APNS(args) { +export function APNS(args) { // Since for ios, there maybe multiple cert/key pairs, // typePushConfig can be an array. let apnsArgsList = []; @@ -187,7 +187,7 @@ function chooseConns(conns, device) { * @param {Object} coreData The data field under api request body * @returns {Object} A apns notification */ -function generateNotification(coreData, expirationTime) { +export function generateNotification(coreData, expirationTime) { let notification = new apn.notification(); let payload = {}; for (let key in coreData) { @@ -224,4 +224,3 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') { APNS.chooseConns = chooseConns; APNS.handleTransmissionError = handleTransmissionError; } -module.exports = APNS; diff --git a/src/Adapters/Push/ParsePushAdapter.js b/src/Adapters/Push/ParsePushAdapter.js index 72cd57ed1b..4ae06cccc4 100644 --- a/src/Adapters/Push/ParsePushAdapter.js +++ b/src/Adapters/Push/ParsePushAdapter.js @@ -4,8 +4,8 @@ // for ios push. const Parse = require('parse/node').Parse; -const GCM = require('../../GCM'); -const APNS = require('../../APNS'); +const GCM = require('../../GCM').GCM; +const APNS = require('../../APNS').APNS; import PushAdapter from './PushAdapter'; import { classifyInstallations } from './PushAdapterUtils'; diff --git a/src/GCM.js b/src/GCM.js index 8068f9d7dd..0f218d0831 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'); @@ -54,7 +54,8 @@ GCM.prototype.send = function(data, devices) { } // Generate gcm payload // PushId is not a formal field of GCM, but Parse Android SDK uses this field to deduplicate push notifications - let gcmPayload = generateGCMPayload(data.data, pushId, timestamp, expirationTime); + let gcmPayload = generateGCMPayload(data.data, null, null, data.expirationTime); + // Make and send gcm request let message = new gcm.Message(gcmPayload); @@ -109,18 +110,23 @@ 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(coreData, pushId, timeStamp, expirationTime) { + pushId = pushId || cryptoUtils.newObjectId(); + timeStamp = timeStamp || Date.now(); + let payloadData = { 'time': new Date(timeStamp).toISOString(), 'push_id': pushId, 'data': JSON.stringify(coreData) } + let payload = { priority: 'normal', data: payloadData }; + if (expirationTime) { - // The timeStamp and expiration is in milliseconds but gcm requires second + // The timeStamp and expiration is in milliseconds but gcm requires second let timeToLive = Math.floor((expirationTime - timeStamp) / 1000); if (timeToLive < 0) { timeToLive = 0; @@ -130,6 +136,7 @@ function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) { } payload.timeToLive = timeToLive; } + return payload; } @@ -151,4 +158,3 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') { GCM.generateGCMPayload = generateGCMPayload; GCM.sliceDevices = sliceDevices; } -module.exports = GCM; diff --git a/src/index.js b/src/index.js index 870f6c5ec1..60bed44942 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,6 @@ import ParseServer from './ParseServer' +import { PushAdapter } from './Adapters/Push/PushAdapter'; +import * as PushAdapterUtils from './Adapters/Push/PushAdapterUtils'; import { GCSAdapter } from './Adapters/Files/GCSAdapter'; import { S3Adapter } from './Adapters/Files/S3Adapter'; import { FileSystemAdapter } from './Adapters/Files/FileSystemAdapter'; @@ -12,4 +14,4 @@ let _ParseServer = function(options) { _ParseServer.createLiveQueryServer = ParseServer.createLiveQueryServer; export default ParseServer; -export { S3Adapter, GCSAdapter, FileSystemAdapter, _ParseServer as ParseServer }; +export { S3Adapter, GCSAdapter, FileSystemAdapter, _ParseServer as ParseServer, PushAdapter, PushAdapterUtils };