Skip to content

SNS Prep work #1158

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 2 commits 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 spec/APNS.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var APNS = require('../src/APNS');
var APNS = require('../src/APNS').APNS;

describe('APNS', () => {

Expand Down
2 changes: 1 addition & 1 deletion spec/GCM.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var GCM = require('../src/GCM');
var GCM = require('../src/GCM').GCM;

describe('GCM', () => {
it('can initialize', (done) => {
Expand Down
4 changes: 2 additions & 2 deletions spec/ParsePushAdapter.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var ParsePushAdapter = require('../src/Adapters/Push/ParsePushAdapter');
var APNS = require('../src/APNS');
var GCM = require('../src/GCM');
var APNS = require('../src/APNS').APNS;
var GCM = require('../src/GCM').GCM;

describe('ParsePushAdapter', () => {
it('can be initialized', (done) => {
Expand Down
5 changes: 2 additions & 3 deletions src/APNS.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const apn = require('apn');
* @param {String} args.bundleId The bundleId for cert
* @param {Boolean} args.production Specifies which environment to connect to: Production (if true) or Sandbox
*/
function APNS(args) {
export function APNS(args) {
// Since for ios, there maybe multiple cert/key pairs,
// typePushConfig can be an array.
let apnsArgsList = [];
Expand Down Expand Up @@ -187,7 +187,7 @@ function chooseConns(conns, device) {
* @param {Object} coreData The data field under api request body
* @returns {Object} A apns notification
*/
function generateNotification(coreData, expirationTime) {
export function generateNotification(coreData, expirationTime) {
let notification = new apn.notification();
let payload = {};
for (let key in coreData) {
Expand Down Expand Up @@ -224,4 +224,3 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
APNS.chooseConns = chooseConns;
APNS.handleTransmissionError = handleTransmissionError;
}
module.exports = APNS;
4 changes: 2 additions & 2 deletions src/Adapters/Push/ParsePushAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// for ios push.

const Parse = require('parse/node').Parse;
const GCM = require('../../GCM');
const APNS = require('../../APNS');
const GCM = require('../../GCM').GCM;
const APNS = require('../../APNS').APNS;
import PushAdapter from './PushAdapter';
import { classifyInstallations } from './PushAdapterUtils';

Expand Down
16 changes: 11 additions & 5 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 Down Expand Up @@ -54,7 +54,8 @@ GCM.prototype.send = function(data, devices) {
}
// Generate gcm payload
// PushId is not a formal field of GCM, but Parse Android SDK uses this field to deduplicate push notifications
let gcmPayload = generateGCMPayload(data.data, pushId, timestamp, expirationTime);
let gcmPayload = generateGCMPayload(data.data, null, null, data.expirationTime);

// Make and send gcm request
let message = new gcm.Message(gcmPayload);

Expand Down Expand Up @@ -109,18 +110,23 @@ 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(coreData, pushId, timeStamp, expirationTime) {
pushId = pushId || cryptoUtils.newObjectId();
timeStamp = timeStamp || Date.now();

let payloadData = {
'time': new Date(timeStamp).toISOString(),
'push_id': pushId,
'data': JSON.stringify(coreData)
}

let payload = {
priority: 'normal',
data: payloadData
};

if (expirationTime) {
// The timeStamp and expiration is in milliseconds but gcm requires second
// The timeStamp and expiration is in milliseconds but gcm requires second
let timeToLive = Math.floor((expirationTime - timeStamp) / 1000);
if (timeToLive < 0) {
timeToLive = 0;
Expand All @@ -130,6 +136,7 @@ function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) {
}
payload.timeToLive = timeToLive;
}

return payload;
}

Expand All @@ -151,4 +158,3 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
GCM.generateGCMPayload = generateGCMPayload;
GCM.sliceDevices = sliceDevices;
}
module.exports = GCM;
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ParseServer from './ParseServer'
import { PushAdapter } from './Adapters/Push/PushAdapter';
import * as PushAdapterUtils from './Adapters/Push/PushAdapterUtils';
import { GCSAdapter } from './Adapters/Files/GCSAdapter';
import { S3Adapter } from './Adapters/Files/S3Adapter';
import { FileSystemAdapter } from './Adapters/Files/FileSystemAdapter';
Expand All @@ -12,4 +14,4 @@ let _ParseServer = function(options) {
_ParseServer.createLiveQueryServer = ParseServer.createLiveQueryServer;

export default ParseServer;
export { S3Adapter, GCSAdapter, FileSystemAdapter, _ParseServer as ParseServer };
export { S3Adapter, GCSAdapter, FileSystemAdapter, _ParseServer as ParseServer, PushAdapter, PushAdapterUtils };