CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-node-gcm

Easy interface for Google's Cloud Messaging service (now Firebase Cloud Messaging)

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

message.mddocs/

Message Creation

Core functionality for creating push notification messages with data payloads, notification content, and delivery options. Messages support both data payloads (processed by app code) and notification payloads (displayed by the system).

Capabilities

Message Constructor

Creates a new message instance with optional configuration.

/**
 * Creates a new Message instance
 * @param {MessageOptions} options - Optional message configuration
 * @returns {Message} Message instance
 */
function Message(options);

// Can be called with or without 'new'
const message1 = new Message();
const message2 = Message({ data: { key: 'value' } });

Usage Examples:

const gcm = require('node-gcm');

// Create empty message
const message = new gcm.Message();

// Create message with initial configuration
const message = new gcm.Message({
    collapseKey: 'update',
    priority: 'high',
    timeToLive: 3600,
    data: {
        userId: '12345',
        action: 'update_profile'
    },
    notification: {
        title: 'Profile Updated',
        body: 'Your profile has been successfully updated',
        icon: 'ic_notification'
    }
});

Data Payload Management

Add custom key-value data to messages for processing by client applications.

/**
 * Add a single data key-value pair
 * @param {string} key - Data key
 * @param {string} value - Data value
 */
message.addData(key, value);

/**
 * Set data payload object (overwrites existing data)
 * @param {Object} data - Data object with string keys and values
 */
message.addData(data);

Usage Examples:

const message = new gcm.Message();

// Add individual data fields
message.addData('userId', '12345');
message.addData('action', 'new_message');
message.addData('messageCount', '3');

// Set data object (overwrites previous data)
message.addData({
    userId: '12345',
    action: 'profile_update',
    timestamp: '1635789012'
});

Notification Payload Management

Add display notification content for system-level notifications.

/**
 * Add a single notification key-value pair
 * @param {string} key - Notification key
 * @param {string} value - Notification value
 */
message.addNotification(key, value);

/**
 * Set notification payload object (overwrites existing notification)
 * @param {NotificationPayload} notification - Notification object
 */
message.addNotification(notification);

Usage Examples:

const message = new gcm.Message();

// Add individual notification fields
message.addNotification('title', 'New Message');
message.addNotification('body', 'You have received a new message');
message.addNotification('icon', 'ic_message');
message.addNotification('sound', 'default');

// Set notification object (overwrites previous notification)
message.addNotification({
    title: 'Alert',
    body: 'System maintenance scheduled',
    icon: 'ic_warning',
    color: '#FF0000',
    click_action: 'MAINTENANCE_ACTIVITY'
});

Message Serialization

Convert message to FCM-compatible JSON format.

/**
 * Convert message to FCM JSON format
 * @returns {Object} FCM-compatible message object
 */
message.toJson();

Usage Example:

const message = new gcm.Message({
    data: { key1: 'value1' },
    notification: { title: 'Hello' },
    priority: 'high'
});

const json = message.toJson();
console.log(json);
// Output:
// {
//   data: { key1: 'value1' },
//   notification: { title: 'Hello' },
//   priority: 'high'
// }

Deprecated Methods

These methods are deprecated but still functional for backward compatibility.

/**
 * @deprecated Use addData(key, value) instead
 */
message.addDataWithKeyValue(key, value);

/**
 * @deprecated Use addData(object) instead
 */
message.addDataWithObject(object);

Message Options

All message configuration options that can be passed to the Message constructor or set via the message instance.

Delivery Options

interface MessageOptions {
    /** Groups messages that can be collapsed, only latest delivered when device comes online */
    collapseKey?: string;
    
    /** Message priority: 'normal' (default) or 'high' */
    priority?: 'normal' | 'high';
    
    /** iOS: wake inactive app when notification received */
    contentAvailable?: boolean;
    
    /** iOS 10+: allow notification content modification via app extension */
    mutableContent?: boolean;
    
    /** Delay message delivery when device is idle */
    delayWhileIdle?: boolean;
    
    /** Message expiration time in seconds (max 4 weeks, default 4 weeks) */
    timeToLive?: number;
    
    /** Restrict delivery to specific package name */
    restrictedPackageName?: string;
    
    /** Test mode - validate request without sending */
    dryRun?: boolean;
    
    /** Custom data payload for app processing */
    data?: { [key: string]: string };
    
    /** System notification payload */
    notification?: NotificationPayload;
    
    /** FCM-specific options */
    fcm_options?: { [key: string]: any };
}

Notification Payload Options

interface NotificationPayload {
    /** Notification title */
    title?: string;
    
    /** Notification body text */
    body?: string;
    
    /** Notification icon resource name */
    icon?: string;
    
    /** Sound to play (filename or 'default') */
    sound?: string;
    
    /** iOS: badge number to display */
    badge?: string;
    
    /** Android: notification tag for replacement */
    tag?: string;
    
    /** Android: notification color in #rrggbb format */
    color?: string;
    
    /** Action when notification clicked */
    click_action?: string;
    
    /** Localization key for body text */
    body_loc_key?: string;
    
    /** Localization arguments for body */
    body_loc_args?: string[];
    
    /** Localization key for title */
    title_loc_key?: string;
    
    /** Localization arguments for title */
    title_loc_args?: string[];
}

Important Notes

  • Data Values: All data payload values must be strings
  • Notification Display: Notifications are only displayed when the app is in the background
  • Size Limits: Total message payload should not exceed 4KB
  • Reserved Keys: Avoid using 'from' in data payload as it may cause FCM errors
  • Platform Differences: Some notification options are platform-specific (iOS vs Android)

docs

constants.md

index.md

message.md

sender.md

tile.json