@@ -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,8 @@ 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 gcmPayload = generateGCMPayload ( data . data , null , null , data . expirationTime ) ;
35
27
// Make and send gcm request
36
28
let message = new gcm . Message ( gcmPayload ) ;
37
29
@@ -68,18 +60,23 @@ GCM.prototype.send = function(data, devices) {
68
60
* @param {Number|undefined } expirationTime A number whose format is the Unix Epoch or undefined
69
61
* @returns {Object } A promise which is resolved after we get results from gcm
70
62
*/
71
- function generateGCMPayload ( coreData , pushId , timeStamp , expirationTime ) {
63
+ export function generateGCMPayload ( coreData , pushId , timeStamp , expirationTime ) {
64
+ pushId = pushId || cryptoUtils . newObjectId ( ) ;
65
+ timeStamp = timeStamp || Date . now ( ) ;
66
+
72
67
let payloadData = {
73
68
'time' : new Date ( timeStamp ) . toISOString ( ) ,
74
69
'push_id' : pushId ,
75
70
'data' : JSON . stringify ( coreData )
76
71
}
72
+
77
73
let payload = {
78
74
priority : 'normal' ,
79
75
data : payloadData
80
76
} ;
77
+
81
78
if ( expirationTime ) {
82
- // The timeStamp and expiration is in milliseconds but gcm requires second
79
+ // The timeStamp and expiration is in milliseconds but gcm requires second
83
80
let timeToLive = Math . floor ( ( expirationTime - timeStamp ) / 1000 ) ;
84
81
if ( timeToLive < 0 ) {
85
82
timeToLive = 0 ;
@@ -89,6 +86,7 @@ function generateGCMPayload(coreData, pushId, timeStamp, expirationTime) {
89
86
}
90
87
payload . timeToLive = timeToLive ;
91
88
}
89
+
92
90
return payload ;
93
91
}
94
92
@@ -110,4 +108,3 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
110
108
GCM . generateGCMPayload = generateGCMPayload ;
111
109
GCM . sliceDevices = sliceDevices ;
112
110
}
113
- module . exports = GCM ;
0 commit comments