Skip to content

Commit 136da6b

Browse files
committed
Refactoring GCM.
1 parent 8883541 commit 136da6b

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

spec/GCM.spec.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

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: 10 additions & 13 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.data, null, null, data.expirationTime);
3527
// Make and send gcm request
3628
let message = new gcm.Message(gcmPayload);
3729

@@ -68,18 +60,23 @@ 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(coreData, pushId, timeStamp, expirationTime) {
64+
pushId = pushId || cryptoUtils.newObjectId();
65+
timeStamp = timeStamp || Date.now();
66+
7267
let payloadData = {
7368
'time': new Date(timeStamp).toISOString(),
7469
'push_id': pushId,
7570
'data': JSON.stringify(coreData)
7671
}
72+
7773
let payload = {
7874
priority: 'normal',
7975
data: payloadData
8076
};
77+
8178
if (expirationTime) {
82-
// The timeStamp and expiration is in milliseconds but gcm requires second
79+
// The timeStamp and expiration is in milliseconds but gcm requires second
8380
let timeToLive = Math.floor((expirationTime - timeStamp) / 1000);
8481
if (timeToLive < 0) {
8582
timeToLive = 0;
@@ -89,6 +86,7 @@ function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) {
8986
}
9087
payload.timeToLive = timeToLive;
9188
}
89+
9290
return payload;
9391
}
9492

@@ -110,4 +108,3 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
110108
GCM.generateGCMPayload = generateGCMPayload;
111109
GCM.sliceDevices = sliceDevices;
112110
}
113-
module.exports = GCM;

0 commit comments

Comments
 (0)