@@ -7,7 +7,7 @@ const cryptoUtils = require('./cryptoUtils');
7
7
const GCMTimeToLiveMax = 4 * 7 * 24 * 60 * 60 ; // GCM allows a max of 4 weeks
8
8
const GCMRegistrationTokensMax = 1000 ;
9
9
10
- function GCM ( args ) {
10
+ export function GCM ( args ) {
11
11
if ( typeof args !== 'object' || ! args . apiKey ) {
12
12
throw new Parse . Error ( Parse . Error . PUSH_MISCONFIGURED ,
13
13
'GCM Configuration is invalid' ) ;
@@ -22,16 +22,9 @@ function GCM(args) {
22
22
* @returns {Object } A promise which is resolved after we get results from gcm
23
23
*/
24
24
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
- }
33
25
// Generate gcm payload
34
- let gcmPayload = generateGCMPayload ( data . data , pushId , timeStamp , expirationTime ) ;
26
+ let gcmData = prepareGCMPayload ( ) ;
27
+ let gcmPayload = generateGCMPayload ( data . data , gcmData . pushId , gcmData . timeStamp , data . expirationTime ) ;
35
28
// Make and send gcm request
36
29
let message = new gcm . Message ( gcmPayload ) ;
37
30
@@ -60,6 +53,17 @@ GCM.prototype.send = function(data, devices) {
60
53
return Parse . Promise . when ( sendPromises ) ;
61
54
}
62
55
56
+ /**
57
+ * Generate default GCM payload data.
58
+ * @returns {Object } Push ID and timestamp
59
+ */
60
+ export function prepareGCMPayload ( ) {
61
+ let pushId = cryptoUtils . newObjectId ( ) ;
62
+ let timeStamp = Date . now ( ) ;
63
+
64
+ return { pushId : pushId , timeStamp : timeStamp } ;
65
+ }
66
+
63
67
/**
64
68
* Generate the gcm payload from the data we get from api request.
65
69
* @param {Object } coreData The data field under api request body
@@ -68,18 +72,21 @@ GCM.prototype.send = function(data, devices) {
68
72
* @param {Number|undefined } expirationTime A number whose format is the Unix Epoch or undefined
69
73
* @returns {Object } A promise which is resolved after we get results from gcm
70
74
*/
71
- function generateGCMPayload ( coreData , pushId , timeStamp , expirationTime ) {
75
+ export function generateGCMPayload ( coreData , pushId , timeStamp , expirationTime ) {
76
+
72
77
let payloadData = {
73
78
'time' : new Date ( timeStamp ) . toISOString ( ) ,
74
79
'push_id' : pushId ,
75
80
'data' : JSON . stringify ( coreData )
76
81
}
82
+
77
83
let payload = {
78
84
priority : 'normal' ,
79
85
data : payloadData
80
86
} ;
87
+
81
88
if ( expirationTime ) {
82
- // The timeStamp and expiration is in milliseconds but gcm requires second
89
+ // The timeStamp and expiration is in milliseconds but gcm requires second
83
90
let timeToLive = Math . floor ( ( expirationTime - timeStamp ) / 1000 ) ;
84
91
if ( timeToLive < 0 ) {
85
92
timeToLive = 0 ;
@@ -89,6 +96,7 @@ function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) {
89
96
}
90
97
payload . timeToLive = timeToLive ;
91
98
}
99
+
92
100
return payload ;
93
101
}
94
102
@@ -110,4 +118,4 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
110
118
GCM . generateGCMPayload = generateGCMPayload ;
111
119
GCM . sliceDevices = sliceDevices ;
112
120
}
113
- module . exports = GCM ;
121
+ // module.exports = GCM;
0 commit comments