raix:push

v3.3.0Published 6 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

raix:push Push notifications

Build Status semantic-release

Push notifications for cordova (ios, android) browser (Chrome, Safari, Firefox) - One unified api on client and server.

Status:

  • APN iOS
  • GCM/FCM Android
  • APN Safari web push (partially implemented)
  • GCM Chrome OS (partially implemented)
  • Firefox OS (partially implemented)
  • BPS Blackberry 10
  • MPNS Windows phone 8
  • MPNS Windows 8
  • ADM Amazon Fire OS
  • Meteor in app notifications

Contributing

We are using semantic-release following the AngularJS Commit Message Conventions - Following this pattern will result in better versioning, better changelog and shorter release cycle.

Install

  $ meteor add raix:push
  $ meteor add cordova:cordova-plugin-device@1.1.5
  $ meteor add cordova:phonegap-plugin-push@1.5.2
  # Note: you probably want to adjust the version numbers to the latest versions of the packages

Getting started

Depending on the platforms you want to work with you will need some credentials or certificates.

Have a look at the Basic example

Theres a good walkthrough by Arthur Carabott

Read the raix:push Newbie Manual by @harryward

Or check out the DEMO by @elvismercado (This example uses the deprecated config.push.json)

Example code for sound (todo: add in docs)

Note: Version 3 uses the cordova npm plugin phonegap-plugin-push

Note: Some of the documentation is outdated, please file an issue or create a pull request - same if you find a bug or want to add tests

Development

Watch the project progress for status or join development

Config

Use the Push.Configure function on client and server.

Client

For example in Meteor.startup() block of main.js

1Push.Configure({
2  android: {
3    senderID: 12341234,
4    alert: true,
5    badge: true,
6    sound: true,
7    vibrate: true,
8    clearNotifications: true
9    // icon: '',
10    // iconColor: ''
11  },
12  ios: {
13    alert: true,
14    badge: true,
15    sound: true
16  }
17});

Additionally you have to touch mobile-config.js

1App.configurePlugin('phonegap-plugin-push', {
2  SENDER_ID: 12341234
3});

This is due to changes in the cordova plugin itself

Server

For example in Meteor.startup() block of main.js

1Push.Configure({
2  apn: {
3    certData: Assets.getText('apnDevCert.pem'),
4    keyData: Assets.getText('apnDevKey.pem'),
5    passphrase: 'xxxxxxxxx',
6    production: true,
7    //gateway: 'gateway.push.apple.com',
8  },
9  gcm: {
10    apiKey: 'xxxxxxx',  // GCM/FCM server key
11  }
12  // production: true,
13  // 'sound' true,
14  // 'badge' true,
15  // 'alert' true,
16  // 'vibrate' true,
17  // 'sendInterval': 15000, Configurable interval between sending
18  // 'sendBatchSize': 1, Configurable number of notifications to send per batch
19  // 'keepNotifications': false,
20//
21});

Note: config.push.json is deprecating

Common API

1    // Push.debug = true; // Add verbosity
2
3    Push.send({
4        from: 'push',
5        title: 'Hello',
6        text: 'world',
7        badge: 1, //optional, use it to set badge count of the receiver when the app is in background.
8        query: {
9            // Ex. send to a specific user if using accounts:
10            userId: 'xxxxxxxxx'
11        } // Query the appCollection
12        // token: appId or token eg. "{ apn: token }"
13        // tokens: array of appId's or tokens
14        // payload: user data
15        // delayUntil: Date
16    });

When in secure mode the client send features require adding allow/deny rules in order to allow the user to send push messages to other users directly from the client - Read more below

Client API

1    Push.id(); // Unified application id - not a token
2    Push.setBadge(count); // ios specific - ignored everywhere else
3    Push.enabled(); // Return true or false
4    Push.enabled(false); // Will disable notifications
5    Push.enabled(true); // Will enable notifications (requires a token...)

Security allow/deny send

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.

1    Push.allow({
2        send: function(userId, notification) {
3            return true; // Allow all users to send
4        }
5    });
6
7    // Or...
8    Push.deny({
9        send: function(userId, notification) {
10            return false; // Allow all users to send
11        }
12    });

Meteor Methods

raix:push-update

Stores a token associated with an application and optionally, a userId.

Parameters:

options - An object containing the necessary data to store a token. Fields:

  • id - String (optional) - a record id for the Application/Token document to update. If this does not exist, will return 404.
  • token - Object - { apn: 'TOKEN' } or { gcm: 'TOKEN' }
  • appName - String - the name of the application to associate the token with
  • userId - String (optional) - the user id so associate with the token and application. If none is included no user will be associated. Use raix:push-setuser to later associate a userId with a token.

Returns:

recordId - The id of the stored document associating appName, token, and optionally user in an object of the form:

{
  result: 'recordId'
}

raix:push-setuser

Associates the current users ID with an Application/Token record based on the given id.

Parameters:

id - String - The ID of the Application/Token record

raix:push-metadata

Adds metadata to a particular Application/Token record.

Parameters

data - Object containing the following fields:

  • id - String - the ID of the Application/Token record to update
  • metadata - Object - The metadata object to add to the Application/Token document

More Info

For more internal or advanced features read ADVANCED.md

Kind regards

Morten (aka RaiX)