src/GCM.js
/**
 * 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.
Generate the gcm payload from the data we get from api request .
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 = ['contentAvailable', 'notification'];
  optionalKeys.forEach((key) => {
    if (requestData.hasOwnProperty(key)) {
      payload[key] = requestData[key];
    }
  });
  if (expirationTime) {
    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 = [ 'contentAvailable' , 'notification' ] ; optionalKeys . forEach ( ( key ) => { if ( requestData . hasOwnProperty ( key ) ) { payload [ key ] = requestData [ key ] ; } } ) ; if ( expirationTime ) { 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.
Generate the gcm payload from the data we get from api request .
// The timeStamp and expiration is in milliseconds but gcm requires second