Skip to content

Commit f0932e9

Browse files
committed
Refactor GCM so it can be reused
1 parent fdb8a2b commit f0932e9

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/Adapters/Push/ParsePushAdapter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// for ios push.
55

66
const Parse = require('parse/node').Parse;
7-
const GCM = require('../../GCM');
7+
const GCM = require('../../GCM').GCM;
88
const APNS = require('../../APNS');
99
import PushAdapter from './PushAdapter';
1010
import { classifyInstallations } from './PushAdapterUtils';

src/GCM.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const cryptoUtils = require('./cryptoUtils');
77
const GCMTimeToLiveMax = 4 * 7 * 24 * 60 * 60; // GCM allows a max of 4 weeks
88
const GCMRegistrationTokensMax = 1000;
99

10-
function GCM(args) {
10+
export function GCM(args) {
1111
if (typeof args !== 'object' || !args.apiKey) {
1212
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
1313
'GCM Configuration is invalid');
@@ -22,16 +22,8 @@ function GCM(args) {
2222
* @returns {Object} A promise which is resolved after we get results from gcm
2323
*/
2424
GCM.prototype.send = function(data, devices) {
25-
let pushId = cryptoUtils.newObjectId();
26-
let timeStamp = Date.now();
27-
let expirationTime;
28-
// We handle the expiration_time convertion in push.js, so expiration_time is a valid date
29-
// in Unix epoch time in milliseconds here
30-
if (data['expiration_time']) {
31-
expirationTime = data['expiration_time'];
32-
}
3325
// Generate gcm payload
34-
let gcmPayload = generateGCMPayload(data.data, pushId, timeStamp, expirationTime);
26+
let gcmPayload = generateGCMPayload(data);
3527
// Make and send gcm request
3628
let message = new gcm.Message(gcmPayload);
3729

@@ -68,7 +60,17 @@ GCM.prototype.send = function(data, devices) {
6860
* @param {Number|undefined} expirationTime A number whose format is the Unix Epoch or undefined
6961
* @returns {Object} A promise which is resolved after we get results from gcm
7062
*/
71-
function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) {
63+
export function generateGCMPayload(data) {
64+
let pushId = cryptoUtils.newObjectId();
65+
let timeStamp = Date.now()
66+
let coreData = data.data;
67+
let expirationTime;
68+
// We handle the expiration_time convertion in push.js, so expiration_time is a valid date
69+
// in Unix epoch time in milliseconds here
70+
if (data['expiration_time']) {
71+
expirationTime = data['expiration_time'];
72+
}
73+
7274
let payloadData = {
7375
'time': new Date(timeStamp).toISOString(),
7476
'push_id': pushId,
@@ -78,6 +80,7 @@ function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) {
7880
priority: 'normal',
7981
data: payloadData
8082
};
83+
8184
if (expirationTime) {
8285
// The timeStamp and expiration is in milliseconds but gcm requires second
8386
let timeToLive = Math.floor((expirationTime - timeStamp) / 1000);
@@ -110,4 +113,4 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
110113
GCM.generateGCMPayload = generateGCMPayload;
111114
GCM.sliceDevices = sliceDevices;
112115
}
113-
module.exports = GCM;
116+
//module.exports = GCM;

0 commit comments

Comments
 (0)