Common:
// The Push object is an EventEmitter
Push.addListener();
Client:
// Internal events
Push.addListener('token', function(token) {
// Token is { apn: 'xxxx' } or { gcm: 'xxxx' }
});
Push.addListener('error', function(err) {
if (err.type == 'apn.cordova') {
console.log(err.error);
}
});
Push.addListener('register', function(evt) {
// Platform specific event - not really used
});
Push.addListener('alert', function(notification) {
// Called when message got a message in forground
});
Push.addListener('sound', function(notification) {
// Called when message got a sound
});
Push.addListener('badge', function(notification) {
// Called when message got a badge
});
Push.addListener('startup', function(notification) {
// Called when message recieved on startup (cold+warm)
});
Push.addListener('message', function(notification) {
// Called on every message
});
The returned notification
object from events:
var notification = {
message,
sound, // Relative to the platform
badge,
coldstart, // True if the app havent been resumed
background, // If message recieved while app was in background
foreground, // If message recieved while app was in foreground
open, // Flag marking if the note triggered the app to open
payload // Custom object
};
Event types:
apn.cordova
gcm.cordova
apn.browser
gcm.browser
cordova.browser
This can be done via:
- In
config.push.json
file - In client/server code
NOTE: config.push.json
is being deprecated and might not work in Meteor 1.3
Add a config.push.json
file in your project and configure credentials / keys / certificates:
{
"apn": {
"passphrase": "xxxxxxxxx",
"key": "apnProdKey.pem",
"cert": "apnProdCert.pem"
},
"gcm": {
"apiKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"projectNumber": xxxxxxxxxxxx
},
"production": true,
// "badge": true,
// "sound": true,
// "alert": true,
// "vibrate": true,
// "sendInterval": 15000, Configurable interval between sending notifications
// "sendBatchSize": 1 Configurable number of notifications to send per batch
}
Please note that Push.Configure
is called automatically when using the config.push.json
file. Push.Configure
may only be called once otherwise it throws an error - this is intended behaviour.
If you want to use the Push.Configure on the client use the options described here
Push.Configure({
gcm: {
apiKey: 'xxxxxxxxxxxxx'
},
apn: {
// setting this on client throws security error
passphrase: 'xxx',
// pem files are placed in the app private folder
certData: Assets.getText('apnProdCert.pem'),
keyData: Assets.getText('apnProdKey.pem'),
},
production: true, // use production server or sandbox
});
// Common client api
Push.Configure({
gcm: {
// Required for Android and Chrome OS
projectNumber: 'xxxxxxxxxxxxxxxxxx'
},
apn: {
// Only required if using safari web push, not required
// for iOS / cordova
websitePushId: 'com.push.server'
webServiceUrl: 'http://some.server.com'
},
bagde: true,
sound: true,
alert: true
});
Push.id(); // Unified application id - not a token
Push.setBadge(count); // ios specific - ignored everywhere else
// Internal events
Push.addListener('token', function(currentToken, newToken) {
// Token is { apn: 'xxxx' } or { gcm: 'xxxx' } or null
// if newToken is null then the currentToken is invalid
// if newToken is set then this should replace the currentToken
});
// Direct access to the send functions
Push.sendAPN(userToken, options);
Push.sendGCM(userTokens, options)
You can send push notifications from the client or the server using Push.send(). If sending from the client you are required to use allow/deny) rules.
There are 4 required parameters that must be passed to Push.send
. They are:
from
: reserved for future use. intended to be internally used by gcm to generate a collapse key. this can be any random string at the momenttitle
: the bold title text that is displayed in the notificationtext
: the normal sub-text that is displayed in the notification- a selection query from below
The 4th parameter is a selection query for determining who the message should be sent to. This query can be one of the three following items:
query
: {} or {userId : 'XXXXX'} or {id : 'XXXXX'}token
: {gcm : 'XXXXXX'} or {apn : 'XXXXX'}tokens
: [{gcm : 'XXXXX0'},{gcm : 'XXXXX1'}, {apn : 'XXXXX0'}]
query
can be left empty in which case the notification will be sent to all devices that have registered a token. query
can also be one or more ids obtained from clients via Push.id()
or one or more userIds associated with the accounts-base package and Meteor.userId().
token
is an apn or gcm token registered by the device in the form:
{ apn: String } or { gcm: String }
tokens
is simply and array of tokens from the previous example
delayUntil
is an optional Date. If set, sending will be delayed until then.
The query selector is used against a Mongo Collection created by the push packahe called Push.appCollection
. This collection stores the userIds, pushIds, and tokens of all devices that register with the server. With a desired selection query chosen a minimal Push.send
takes the following form (using one of the queries).
Push.send({
from: 'Test',
title: 'Hello',
text: 'World',
query: {}
token: {}
tokens: [{},{}]
delayUntil: new Date(),
notId: numberId
});
notId
: a unique identifier for a GCM message
'notId' supplies a unique id to Cordova Push plugin for 'tag' field in GCM (Android) allowing a per message id, this can be used to replace unread message on both server and client. It differs from collapseKey which only collapses undelivered messages server side. Defaults to a value of zero, must be 32 bit Integer
If notId
is not set then the Push plugin defaults to a value of 0 causing each message to overwrite the previous and only ever display a single notification.
If needed it's possible to specify values pr. platform apn
/gcm
in the send.
Eg.:
Push.send({
from: 'Test',
title: 'Hello',
text: 'World',
apn: {
// apn specific overwrites
title: 'sent via apn'
},
gcm: {
// gcm specific overwrites
title: 'sent via gcm'
},
query: {}
token: {}
tokens: [{},{}]
delayUntil: new Date()
});
You can overwrite keys: 'from','title','text','badge','sound' and 'notId'
Push.send({
from: 'Test',
title: 'Large icon',
text: 'Remotely loaded',
gcm: {
// gcm specific overwrites
image: 'https://c1.staticflickr.com/9/8079/8391224056_96da82499d_n.jpg'
}
});
Produces the following result:
First notification:
Push.send({
from: 'Test',
title: 'My Title',
text: 'My first message',
gcm: {
style: 'inbox',
summaryText: 'There are %n% notifications'
}
});
Produces the following result:
Second notification:
Push.send({
from: 'Test',
title: 'My Title',
text: 'My second message',
gcm: {
style: 'inbox',
summaryText: 'There are %n% notifications'
}
});
Produces the following result:
To include a large picture in the notification:
Push.send({
from: 'Test',
title: 'Big Picture',
text: 'This is my big picture message',
gcm: {
style: 'picture',
picture: 'http://36.media.tumblr.com/c066cc2238103856c9ac506faa6f3bc2/tumblr_nmstmqtuo81tssmyno1_1280.jpg',
summaryText: 'The internet is built on cat pictures'
}
});
Produces the following result:
This package allows you to send notifications from the server and client. To restrict the client or allowing the client to send use allow
or deny
rules.
When a client calls send on Push, the Push's allow and deny callbacks are called on the server to determine if the send should be allowed. If at least one allow callback allows the send, and no deny callbacks deny the send, then the send is allowed to proceed.
Push.allow({
send: function(userId, notification) {
return true; // Allow all users to send
}
});
// Or...
Push.deny({
send: function(userId, notification) {
return false; // Allow all users to send
}
});