Skip to content

Commit e8fe9fd

Browse files
committed
SNS Prep work
Need to reuse APNS and GCM code.
1 parent 24f0fff commit e8fe9fd

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

spec/APNS.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var APNS = require('../src/APNS');
1+
var APNS = require('../src/APNS').APNS;
22

33
describe('APNS', () => {
44

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var ParsePushAdapter = require('../src/Adapters/Push/ParsePushAdapter');
2-
var APNS = require('../src/APNS');
3-
var GCM = require('../src/GCM');
2+
var APNS = require('../src/APNS').APNS;
3+
var GCM = require('../src/GCM').GCM;
44

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

src/APNS.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const apn = require('apn');
1616
* @param {String} args.bundleId The bundleId for cert
1717
* @param {Boolean} args.production Specifies which environment to connect to: Production (if true) or Sandbox
1818
*/
19-
function APNS(args) {
19+
export function APNS(args) {
2020
// Since for ios, there maybe multiple cert/key pairs,
2121
// typePushConfig can be an array.
2222
let apnsArgsList = [];
@@ -187,7 +187,7 @@ function chooseConns(conns, device) {
187187
* @param {Object} coreData The data field under api request body
188188
* @returns {Object} A apns notification
189189
*/
190-
function generateNotification(coreData, expirationTime) {
190+
export function generateNotification(coreData, expirationTime) {
191191
let notification = new apn.notification();
192192
let payload = {};
193193
for (let key in coreData) {
@@ -224,4 +224,3 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
224224
APNS.chooseConns = chooseConns;
225225
APNS.handleTransmissionError = handleTransmissionError;
226226
}
227-
module.exports = APNS;

src/Adapters/Push/ParsePushAdapter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// for ios push.
55

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

src/GCM.js

Lines changed: 10 additions & 5 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');
@@ -52,7 +52,8 @@ GCM.prototype.send = function(data, devices) {
5252
expirationTime = data['expiration_time'];
5353
}
5454
// Generate gcm payload
55-
let gcmPayload = generateGCMPayload(data.data, timestamp, expirationTime);
55+
let gcmPayload = generateGCMPayload(data.data, null, data.expirationTime);
56+
5657
// Make and send gcm request
5758
let message = new gcm.Message(gcmPayload);
5859

@@ -107,17 +108,21 @@ GCM.prototype.send = function(data, devices) {
107108
* @param {Number|undefined} expirationTime A number whose format is the Unix Epoch or undefined
108109
* @returns {Object} A promise which is resolved after we get results from gcm
109110
*/
110-
function generateGCMPayload(coreData, timeStamp, expirationTime) {
111+
export function generateGCMPayload(coreData, timeStamp, expirationTime) {
112+
timeStamp = timeStamp || Date.now();
113+
111114
let payloadData = {
112115
'time': new Date(timeStamp).toISOString(),
113116
'data': JSON.stringify(coreData)
114117
}
118+
115119
let payload = {
116120
priority: 'normal',
117121
data: payloadData
118122
};
123+
119124
if (expirationTime) {
120-
// The timeStamp and expiration is in milliseconds but gcm requires second
125+
// The timeStamp and expiration is in milliseconds but gcm requires second
121126
let timeToLive = Math.floor((expirationTime - timeStamp) / 1000);
122127
if (timeToLive < 0) {
123128
timeToLive = 0;
@@ -127,6 +132,7 @@ function generateGCMPayload(coreData, timeStamp, expirationTime) {
127132
}
128133
payload.timeToLive = timeToLive;
129134
}
135+
130136
return payload;
131137
}
132138

@@ -148,4 +154,3 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
148154
GCM.generateGCMPayload = generateGCMPayload;
149155
GCM.sliceDevices = sliceDevices;
150156
}
151-
module.exports = GCM;

0 commit comments

Comments
 (0)