src/GCM.js
function generateGCMPayload(requestData, pushId, timeStamp, expirationTime) {
  let payload = {
    priority: 'high'
  };
  payload.data = {
    data: requestData.data,
    push_id: pushId,
    time: new Date(timeStamp).toISOString()
  }
  const optionalKeys = ['content_available', 'notification'];
  optionalKeys.forEach((key, index, array) => {
    if (requestData.hasOwnProperty(key)) {
      payload[key] = requestData[key];
    }
  });

  if (expirationTime) {
   // The timeStamp and expiration is in milliseconds but gcm requires second
    let timeToLive = Math.floor((expirationTime - timeStamp) / 1000);
    if (timeToLive < 0) {
      timeToLive = 0;
    }
    if (timeToLive >= GCMTimeToLiveMax) {
      timeToLive = GCMTimeToLiveMax;
    }
    payload.timeToLive = timeToLive;
  }
  return payload;
}
function generateGCMPayload(requestData, pushId, timeStamp, expirationTime) {
  let payload = {
    priority: 'high'
  };
  payload.data = {
    data: requestData.data,
    push_id: pushId,
    time: new Date(timeStamp).toISOString()
  }
  const optionalKeys = ['content_available', 'notification'];
  optionalKeys.forEach((key, index, array) => {
    if (requestData.hasOwnProperty(key)) {
      payload[key] = requestData[key];
    }
  });

  if (expirationTime) {
   // The timeStamp and expiration is in milliseconds but gcm requires second
    let timeToLive = Math.floor((expirationTime - timeStamp) / 1000);
    if (timeToLive < 0) {
      timeToLive = 0;
    }
    if (timeToLive >= GCMTimeToLiveMax) {
      timeToLive = GCMTimeToLiveMax;
    }
    payload.timeToLive = timeToLive;
  }
  return payload;
}
function generateGCMPayload ( requestData , pushId , timeStamp , expirationTime ) { let payload = { priority : 'high' } ; payload . data = { data : requestData . data , push_id : pushId , time : new Date ( timeStamp ) . toISOString ( ) } const optionalKeys = [ 'content_available' , 'notification' ] ; optionalKeys . forEach ( ( key , index , array ) => { if ( requestData . hasOwnProperty ( key ) ) { payload [ key ] = requestData [ key ] ; } } ) ; if ( expirationTime ) { // The timeStamp and expiration is in milliseconds but gcm requires second let timeToLive = Math . floor ( ( expirationTime - timeStamp ) / 1000 ) ; if ( timeToLive < 0 ) { timeToLive = 0 ; } if ( timeToLive >= GCMTimeToLiveMax ) { timeToLive = GCMTimeToLiveMax ; } payload . timeToLive = timeToLive ; } return payload ; }
Generate the gcm payload from the data we get from api request.
@param {Object} requestData The request body
@param {String} pushId A random string
@param {Number} timeStamp A number whose format is the Unix Epoch
@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
Generate the gcm payload from the data we get from api request .