gcm-server
gcm-server is a Node.JS library that start a Server to store the GCM clients Subscription ID. You can also send push notifications (thanks to node-gcm).
Installation
npm install gcm-server --save
.
Requirements
This library provides the server-side implementation of GCM. You need to generate an API key (Sender ID).
GCM notifications can be sent to both Android and iOS. If you are new to GCM you should probably look into the documentation.
Server usage
Here a basic example of how to start the server and listen for new clients Subscription ID.
var gcm = ; // set the new server and the entry points before any app.listen.var GcmServer = port: 3000; GcmServer; GcmServer; GcmServerstart;
Available entry points
- newTokenEntryPoint - POST: '/gcm/token/new'
- deleteTokenEntryPoint - GET: '/gcm/token/delete/:token'
- editTokenEntryPoint - POST: '/gcm/token/edit'
- getTokenEntryPoint - GET: '/gcm/token/get'
GCM send - Example application
According to below Usage reference, we could create such application:
var gcm = ; var message = ; message; var regTokens = 'YOUR_REG_TOKEN_HERE'; // Set up the sender with you API keyvar sender = 'YOUR_API_KEY_HERE'; // Now the sender can be used to send messagessender; // Send to a topic, with no retry this timesender;
Usage
var gcm = ; // Create a message// ... with default valuesvar message = ; // ... or some given valuesvar message = collapseKey: 'demo' priority: 'high' contentAvailable: true delayWhileIdle: true timeToLive: 3 restrictedPackageName: "somePackageName" dryRun: true data: key1: 'message1' key2: 'message2' notification: title: "Hello, World" icon: "ic_launcher" body: "This is a notification that will be displayed ASAP." ; // Change the message data// ... as key-valuemessage;message; // ... or as a data object (overwrites previous data object)message; // Set up the sender with you API keyvar sender = 'insert Google Server API Key here'; // Add the registration tokens of the devices you want to send tovar registrationTokens = ;registrationTokens;registrationTokens; // Send the message// ... trying only oncesender; // ... or retryingsender; // ... or retrying a specific number of times (10)sender;
More about sending push notifications.
See node-gcm for more references.