Skip to content

Refactor GCM so it can be reused #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Adapters/Push/ParsePushAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// for ios push.

const Parse = require('parse/node').Parse;
const GCM = require('../../GCM');
const GCM = require('../../GCM').GCM;
const APNS = require('../../APNS');
import PushAdapter from './PushAdapter';
import { classifyInstallations } from './PushAdapterUtils';
Expand Down
26 changes: 14 additions & 12 deletions src/GCM.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -22,16 +22,8 @@ function GCM(args) {
* @returns {Object} A promise which is resolved after we get results from gcm
*/
GCM.prototype.send = function(data, devices) {
let pushId = cryptoUtils.newObjectId();
let timeStamp = Date.now();
let expirationTime;
// We handle the expiration_time convertion in push.js, so expiration_time is a valid date
// in Unix epoch time in milliseconds here
if (data['expiration_time']) {
expirationTime = data['expiration_time'];
}
// Generate gcm payload
let gcmPayload = generateGCMPayload(data.data, pushId, timeStamp, expirationTime);
let gcmPayload = generateGCMPayload(data);
// Make and send gcm request
let message = new gcm.Message(gcmPayload);

Expand Down Expand Up @@ -68,7 +60,17 @@ 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(data) {
let pushId = cryptoUtils.newObjectId();
let timeStamp = Date.now()
let coreData = data.data;
let expirationTime;
// We handle the expiration_time convertion in push.js, so expiration_time is a valid date
// in Unix epoch time in milliseconds here
if (data['expiration_time']) {
expirationTime = data['expiration_time'];
}

let payloadData = {
'time': new Date(timeStamp).toISOString(),
'push_id': pushId,
Expand All @@ -78,6 +80,7 @@ function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) {
priority: 'normal',
data: payloadData
};

if (expirationTime) {
// The timeStamp and expiration is in milliseconds but gcm requires second
let timeToLive = Math.floor((expirationTime - timeStamp) / 1000);
Expand Down Expand Up @@ -110,4 +113,3 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
GCM.generateGCMPayload = generateGCMPayload;
GCM.sliceDevices = sliceDevices;
}
module.exports = GCM;