Skip to content

Commit 55cc564

Browse files
committed
Refactoring GCM.
1 parent 7194fb1 commit 55cc564

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
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: 12 additions & 4 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');
@@ -53,6 +53,10 @@ GCM.prototype.send = function(data, devices) {
5353
}
5454
// Generate gcm payload
5555
let gcmPayload = generateGCMPayload(data.data, timestamp, expirationTime);
56+
=======
57+
// Generate gcm payload
58+
let gcmPayload = generateGCMPayload(data.data, null, null, data.expirationTime);
59+
>>>>>>> Refactoring GCM.
5660
// Make and send gcm request
5761
let message = new gcm.Message(gcmPayload);
5862

@@ -107,17 +111,21 @@ GCM.prototype.send = function(data, devices) {
107111
* @param {Number|undefined} expirationTime A number whose format is the Unix Epoch or undefined
108112
* @returns {Object} A promise which is resolved after we get results from gcm
109113
*/
110-
function generateGCMPayload(coreData, timeStamp, expirationTime) {
114+
export function generateGCMPayload(coreData, timeStamp, expirationTime) {
115+
timeStamp = timeStamp || Date.now();
116+
111117
let payloadData = {
112118
'time': new Date(timeStamp).toISOString(),
113119
'data': JSON.stringify(coreData)
114120
}
121+
115122
let payload = {
116123
priority: 'normal',
117124
data: payloadData
118125
};
126+
119127
if (expirationTime) {
120-
// The timeStamp and expiration is in milliseconds but gcm requires second
128+
// The timeStamp and expiration is in milliseconds but gcm requires second
121129
let timeToLive = Math.floor((expirationTime - timeStamp) / 1000);
122130
if (timeToLive < 0) {
123131
timeToLive = 0;
@@ -127,6 +135,7 @@ function generateGCMPayload(coreData, timeStamp, expirationTime) {
127135
}
128136
payload.timeToLive = timeToLive;
129137
}
138+
130139
return payload;
131140
}
132141

@@ -148,4 +157,3 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
148157
GCM.generateGCMPayload = generateGCMPayload;
149158
GCM.sliceDevices = sliceDevices;
150159
}
151-
module.exports = GCM;

0 commit comments

Comments
 (0)