1.0.2 • Published 8 years ago

parse-to-anh v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

parse-to-anh

Node.js module and tool to migrate android and ios users from parse to Azure Notification Hubs. The script updates the _Installation collection with a new property anhRegistrationId that can later be used to send puah notifications for the user.

Sending a notification to a user is done by sending a notification to a tag of the user's anhRegistrationId. Example below.

In addition, you can provide a handler function that will get the RegistrationId and run your own logic. For example, if you wish to save this registrationId in another collection, or provision it against an addition API.

First thing first

  • First you'll need to export Parse db to your own mongo db instance. Deatils here
  • Create an Azure Notification Hub as explained here

Usage- As a Tool

npm install parse-to-anh -g

Create a configuration file: config.json:

{
  "mongoUrl": "yourMongoUrl",
  "hubName": "yourAzureHubName",
  "hubConnection": "yourAzureHubConnectionString",
  "tags": [],
  "reRegister": true,
  "batchSize": 100,
  "limit": 0
}

options

  • mongoUrl- The Url for your mongo db that was migrated from parse
  • hubName- The Azure Notification Hub name
  • hubConnection- The Azure Notification Hub connection string
  • tags- Default list of tags that will be added to the user
  • reRegister- Optional, default false ReRegister users that were already registered. This can be used if for some reason you run the script again on a db that was only partially processed.
  • batchSize- Optional The size of the batch used by mongo client
  • limit- Optional Use that to limit the number of items to process. Can be used to test with a small amount of items, before processing the whole collection.

  • registrationHandler- Optional- a callback function that gets an _Installation document containing the new anhRegistrationId field. This handler will be called for each successfull user registration. This is the place for you to add your own custom code to handle the registrationId.

Executing

run:

parse-to-anh config.json

Grab a coffee and relax...

Usage- As a module

Check the sample file for a reference

var migrator = require('parse-to-anh');
var config = {
 "mongoUrl": "yourMongoUrl",
  "hubName": "yourAzureHubName",
  "hubConnection": "yourAzureHubConnectionString",
  "tags": [],
  "reRegister": true,
  "batchSize": 100,
  "limit": 0,
  "registrationHandler": myHandler
};


function myHandler(installation, cb) {
  // TODO: custom code to use installation.anhRegistrationId 
  // and the rest of the properties of the installation document
  return cb();
}

console.log('starting registering to Azure Notification Hub...');

migrator.start(config, function (err) {
  if (err) return console.error('error occurred:', err);
  console.log('migration completed successfully');
});

Sending a notification to a user from your node.js server

Use the anhRegistrationId to send a notification to the user. The below example assumes an ios user, using apns to send the notification:

var azure = require('azure');
var hubService = azure.createNotificationHubService(hubName, hubConn);

var payload = {
  alert: 'Hello from Azure Notification Hubs!'
};

var tags = [anhRegistrationId];

hubService.apns.send(tags, payload, function (err) {
    if (err) return console.error('error sending notification', err);
    console.log('Success: notification sent');
});

Notes

  • Currently supports ios and android
  • Failed users will not stop the process. An error log will be sent to the console.

License

MIT