Easy interface for Google's Cloud Messaging service (now Firebase Cloud Messaging)
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Configuration constants for FCM endpoints, timeouts, and error handling. These constants are used internally by the library but can be referenced for debugging and configuration purposes.
Core FCM service endpoints and connection settings.
const Constants = require('node-gcm').Constants;
/** FCM service endpoint hostname */
Constants.GCM_SEND_ENDPOINT; // 'fcm.googleapis.com'
/** FCM service endpoint path */
Constants.GCM_SEND_ENDPATH; // '/fcm/send'
/** Complete FCM service URL */
Constants.GCM_SEND_URI; // 'https://fcm.googleapis.com/fcm/send'Network timeout and retry behavior settings.
/** Socket timeout in milliseconds (3 minutes) */
Constants.SOCKET_TIMEOUT; // 180000
/** Initial delay for retry backoff in milliseconds */
Constants.BACKOFF_INITIAL_DELAY; // 1000
/** Maximum backoff delay in milliseconds */
Constants.MAX_BACKOFF_DELAY; // 1024000Usage Example:
const gcm = require('node-gcm');
// Create sender with custom timeout based on constants
const sender = new gcm.Sender('YOUR_API_KEY', {
timeout: gcm.Constants.SOCKET_TIMEOUT / 2 // Half the default timeout
});
// Use constants for custom retry logic
const customBackoff = gcm.Constants.BACKOFF_INITIAL_DELAY * 2;FCM error codes for response handling and debugging.
/** Quota exceeded for sender */
Constants.ERROR_QUOTA_EXCEEDED; // 'QuotaExceeded'
/** Device quota exceeded */
Constants.ERROR_DEVICE_QUOTA_EXCEEDED; // 'DeviceQuotaExceeded'
/** Registration token missing from request */
Constants.ERROR_MISSING_REGISTRATION; // 'MissingRegistration'
/** Invalid registration token format */
Constants.ERROR_INVALID_REGISTRATION; // 'InvalidRegistration'
/** Registration token doesn't match sender ID */
Constants.ERROR_MISMATCH_SENDER_ID; // 'MismatchSenderId'
/** Registration token is no longer valid */
Constants.ERROR_NOT_REGISTERED; // 'NotRegistered'
/** Message payload exceeds size limit */
Constants.ERROR_MESSAGE_TOO_BIG; // 'MessageTooBig'
/** Collapse key missing for required message type */
Constants.ERROR_MISSING_COLLAPSE_KEY; // 'MissingCollapseKey'
/** FCM service temporarily unavailable */
Constants.ERROR_UNAVAILABLE; // 'Unavailable'
/** FCM internal server error */
Constants.ERROR_INTERNAL_SERVER_ERROR; // 'InternalServerError'Usage Example:
const gcm = require('node-gcm');
sender.send(message, recipients, function(err, response) {
if (response && response.results) {
response.results.forEach((result, index) => {
if (result.error) {
switch (result.error) {
case gcm.Constants.ERROR_NOT_REGISTERED:
console.log(`Token ${index} is no longer registered`);
// Remove token from database
break;
case gcm.Constants.ERROR_INVALID_REGISTRATION:
console.log(`Token ${index} has invalid format`);
// Remove invalid token
break;
case gcm.Constants.ERROR_MESSAGE_TOO_BIG:
console.log('Message payload too large');
// Reduce message size
break;
case gcm.Constants.ERROR_QUOTA_EXCEEDED:
console.log('Sending quota exceeded');
// Implement rate limiting
break;
default:
console.log(`Unknown error: ${result.error}`);
}
}
});
}
});These constants are marked as deprecated but still available for backward compatibility.
// JSON field names - used internally for response parsing
Constants.TOKEN_MESSAGE_ID; // 'id'
Constants.TOKEN_CANONICAL_REG_ID; // 'registration_id'
Constants.TOKEN_ERROR; // 'Error'
Constants.JSON_REGISTRATION_IDS; // 'registration_ids'
Constants.JSON_PAYLOAD; // 'data'
Constants.JSON_NOTIFICATION; // 'notification'
Constants.JSON_SUCCESS; // 'success'
Constants.JSON_FAILURE; // 'failure'
Constants.JSON_CANONICAL_IDS; // 'canonical_ids'
Constants.JSON_MULTICAST_ID; // 'multicast_id'
Constants.JSON_RESULTS; // 'results'
Constants.JSON_ERROR; // 'error'
Constants.JSON_MESSAGE_ID; // 'message_id'
Constants.UTF8; // 'UTF-8'GCM_SEND_ENDPOINT - FCM hostnameGCM_SEND_ENDPATH - FCM API pathGCM_SEND_URI - Complete FCM URLSOCKET_TIMEOUT - Request timeoutBACKOFF_INITIAL_DELAY - Initial retry delayMAX_BACKOFF_DELAY - Maximum retry delayERROR_* constants - FCM error code stringsTOKEN_* and JSON_* constants - Response field namesconst { Sender, Message, Constants } = require('node-gcm');
function handleSendResponse(response, originalTokens) {
const tokensToRemove = [];
const tokensToUpdate = [];
response.results.forEach((result, index) => {
const token = originalTokens[index];
if (result.error) {
switch (result.error) {
case Constants.ERROR_NOT_REGISTERED:
case Constants.ERROR_INVALID_REGISTRATION:
tokensToRemove.push(token);
break;
case Constants.ERROR_UNAVAILABLE:
case Constants.ERROR_INTERNAL_SERVER_ERROR:
// These are retried automatically by the library
console.log(`Temporary error for token ${token}`);
break;
}
} else if (result.registration_id) {
tokensToUpdate.push({
oldToken: token,
newToken: result.registration_id
});
}
});
return { tokensToRemove, tokensToUpdate };
}const sender = new Sender('YOUR_API_KEY', {
// Use half the default socket timeout
timeout: Constants.SOCKET_TIMEOUT / 2,
// Custom retry configuration
retries: 3,
backoff: Constants.BACKOFF_INITIAL_DELAY * 1.5
});TOKEN_* and JSON_* constants in new codeBACKOFF_INITIAL_DELAY and MAX_BACKOFF_DELAY