Skip to content

Commit bbcaa34

Browse files
committed
Refactor GCM so it can be reused
More refactoring of GCM module. Fix GCM refactor
1 parent 8883541 commit bbcaa34

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

spec/GCM.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var GCM = require('../src/GCM');
1+
var GCM = require('../src/GCM').GCM;
22

33
describe('GCM', () => {
44
it('can initialize', (done) => {

spec/ParsePushAdapter.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var ParsePushAdapter = require('../src/Adapters/Push/ParsePushAdapter');
22
var APNS = require('../src/APNS');
3-
var GCM = require('../src/GCM');
3+
var GCM = require('../src/GCM').GCM;
44

55
describe('ParsePushAdapter', () => {
66
it('can be initialized', (done) => {

src/Adapters/Push/ParsePushAdapter.js

+1-1
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

+21-13
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,9 @@ 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 gcmData = prepareGCMPayload();
27+
let gcmPayload = generateGCMPayload(data.data, gcmData.pushId, gcmData.timeStamp, data.expirationTime);
3528
// Make and send gcm request
3629
let message = new gcm.Message(gcmPayload);
3730

@@ -60,6 +53,17 @@ GCM.prototype.send = function(data, devices) {
6053
return Parse.Promise.when(sendPromises);
6154
}
6255

56+
/**
57+
* Generate default GCM payload data.
58+
* @returns {Object} Push ID and timestamp
59+
*/
60+
export function prepareGCMPayload() {
61+
let pushId = cryptoUtils.newObjectId();
62+
let timeStamp = Date.now();
63+
64+
return {pushId: pushId, timeStamp: timeStamp};
65+
}
66+
6367
/**
6468
* Generate the gcm payload from the data we get from api request.
6569
* @param {Object} coreData The data field under api request body
@@ -68,18 +72,21 @@ GCM.prototype.send = function(data, devices) {
6872
* @param {Number|undefined} expirationTime A number whose format is the Unix Epoch or undefined
6973
* @returns {Object} A promise which is resolved after we get results from gcm
7074
*/
71-
function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) {
75+
export function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) {
76+
7277
let payloadData = {
7378
'time': new Date(timeStamp).toISOString(),
7479
'push_id': pushId,
7580
'data': JSON.stringify(coreData)
7681
}
82+
7783
let payload = {
7884
priority: 'normal',
7985
data: payloadData
8086
};
87+
8188
if (expirationTime) {
82-
// The timeStamp and expiration is in milliseconds but gcm requires second
89+
// The timeStamp and expiration is in milliseconds but gcm requires second
8390
let timeToLive = Math.floor((expirationTime - timeStamp) / 1000);
8491
if (timeToLive < 0) {
8592
timeToLive = 0;
@@ -89,6 +96,7 @@ function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) {
8996
}
9097
payload.timeToLive = timeToLive;
9198
}
99+
92100
return payload;
93101
}
94102

@@ -110,4 +118,4 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
110118
GCM.generateGCMPayload = generateGCMPayload;
111119
GCM.sliceDevices = sliceDevices;
112120
}
113-
module.exports = GCM;
121+
//module.exports = GCM;

0 commit comments

Comments
 (0)