parse-to-anh
v1.0.2
Published
migrates parse users to Azure Notification Hubs
Downloads
9
Maintainers
Readme
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 parsehubName
- The Azure Notification Hub namehubConnection
- The Azure Notification Hub connection stringtags
- Default list of tags that will be added to the userreRegister
- [Optional, defaultfalse
] 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 clientlimit
- [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 newanhRegistrationId
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.