npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

batch-notifications

v0.3.1

Published

NodeJS client wrapper for the Batch.com notifications server API

Downloads

34

Readme

Batch.com SDK for Node

Build Status Coverage Status Known Vulnerabilities

This project is a NodeJS client wrapper for the Batch.com notifications server API.

Prerequisites

Every methods in this library in Promise-based using the native object. Moreover, since we're using some of the ES2015+ operators (i.e. const, arrow functions, etc), you'll need at least node 6.

Installation

# npm
$ npm install --save batch-notifications
# yarn
$ yarn install batch-notifications

Usage

Options

Before doing anything, you must pass your API keys created for your app in batch:

const opts = {
    restKey: "YOUR_REST_API_KEY",
    // you must pass at least one the following API key
    devKey: "YOUR_DEV_API_KEY",
    liveKey: "YOUR_LIVE_API_KEY",
    // optional
    // by default a bunyan instance will be created
    logger: new MyCustomLogger()
};

The client will always favor the liveKey if present otherwise it will take the devKey.

API

The following sections are presenting the API methods.

Campaigns

See the available parameters in the batch documentation.

create

For example, by taking the minimal payload example, we can create a new campaign like the following:

// see `opts` structure above
const batch = require("batch-notifications")(opts);

const payload = {
    name: "Test Campaign",
    push_time: "now",
    live: false,
    messages: [
        {
            language: "en",
            title: "Hello!",
            body: "How's it going?"
        }
    ]
};

batch.campaign.create(payload)
.then(function (campaignToken) {
    // created campaign referenced by the `campaignToken`
});
update

Updating a campaign is somewhat similar as create a new one except that you'll have some restrictions:

const batch = require("batch-notifications")(opts);

const payload = {
    push_time: "2038-01-19T03:14:07"
};

// token was taken from the previous `create` method
batch.campaign.update(token, payload)
.then(function () {
    // campaign updated
});
remove

Removing a campaign is done like this:

const batch = require("batch-notifications")(opts);

// token was taken from the previous `create` method
batch.campaign.remove(token)
.then(function () {
    // campaign removed
});
stats

Stats can only be fetched if the campaign token was created using the live key and that the campaign is already launched:

// see `opts` structure above
const batch = require("batch-notifications")(opts);

// token was taken from the previous `create` method
batch.campaign.stats(token)
.then(function (detail) {
    // ...
});

The detail object will contain the following properties:

  • date: Date
  • sent: Number
  • direct_open: Number
  • influenced_open: Number
  • open_rate: Number
  • reengaged: Number
  • errors: Number

See the docs for more infos about those variables.

get

Retrieve details about one campaign:

// see `opts` structure above
const batch = require("batch-notifications")(opts);

// token was taken from the previous `create` method
batch.campaign.get(token)
.then(function (details) {
    // ...
});

The details object will contain at least the following properties:

  • campaign_token: String
  • from_api: Boolean
  • dev_only: Boolean
  • created_date: Date
  • name: String
  • live: Boolean
  • push_time: Date
list

Get a paginated list of pushed campaigns:

// see `opts` structure above
const batch = require("batch-notifications")(opts);

// by default, it will fetch at most 10 campaigns
batch.campaign.list()
.then(function (detailsList) {
    // ...
});

The detailsList array is composed of objects which contain the same properties as get.

has

To verify that a token is present for a certain app:

// see `opts` structure above
const batch = require("batch-notifications")(opts);

// token was taken from the previous `create` method
batch.campaign.has(token)
.then(function (hasToken) {
    // ...
});

hasToken is a boolean which is true only if the token is present for the specific API key.

enable

To ensure that a campaign is enabled:

// see `opts` structure above
const batch = require("batch-notifications")(opts);

// token was taken from the previous `create` method
batch.campaign.enable(token)
.then(function () {
    // campaign is enabled
});
disable

To ensure that a campaign is disabled:

// see `opts` structure above
const batch = require("batch-notifications")(opts);

// token was taken from the previous `create` method
batch.campaign.disable(token)
.then(function () {
    // campaign is disabled
});

Transactional

See the available parameters in the batch documentation.

post

For example, by taking the minimal payload example, we can create a new transactional notification like the following:

// see `opts` structure above
const batch = require("batch-notifications")(opts);

const payload = {
    "group_id": "welcome",
    "recipients": {
        "tokens": ["USER_PUSH_TOKEN"]
    },
    "message": {
        "title": "Hello!",
        "body": "How's it going?"
    }
};

batch.transactional.post(payload)
.then(function (token) {
    // created transactional referenced by the `token`
});

Custom Data

save

To set custom data to a specific user (you need their id):

// see `opts` structure above
const batch = require("batch-notifications")(opts);

const payload = {
    "overwrite": false,
    "values":
    {
        "u.nickname": "The Rock",
        "u.force": 42,
        "ut.hobbies": ["Lifting", "Wrestling", "Acting"],
        "u.is_subscribed": null,
        "date(u.last_subscription)": "2016-01-10T10:00:00.000",
        "date(u.last_purchase)": 1472656161,
        "ut.locations": { "$add": ["Paris"], "$remove": ["Berlin"] }
    }
};

// userId (String) is the Id of the user to whom you want to set custom data
batch.customData.save(userId, payload)
.then(function (token) {
    // the `token` represents the transaction
});
saveBulk

To set custom data to several users:

// see `opts` structure above
const batch = require("batch-notifications")(opts);

const payload = [
    {
        "id": "Vincent",
        "update":
        {
            "values":
            {
                "u.nickname": "Vincent",
                "u.age": 55
            }
        }
    },
    {
        "id": "Johnny",
        "update":
        {
            "overwrite": true,
            "values":
            {
                "u.nickname": "BeGood",
                "u.age": 30
            }
        }
    }
];

batch.customData.saveBulk(payload)
.then(function (token) {
    // the `token` represents the transaction
});
delete

To delete custom data of a specific user (you need their id):

// see `opts` structure above
const batch = require("batch-notifications")(opts);

// userId (String) is the Id of the user to whom you want to delete custom data
// this method does not require a payload
batch.customData.delete(userId)
.then(function (token) {
    // the `token` represents the transaction
});
deleteBulk

To delete custom data of an array of users:

// see `opts` structure above
const batch = require("batch-notifications")(opts);

const payload = [
    "user1",
    "user2",
    "user3"
];

batch.customData.deleteBulk(payload)
.then(function (token) {
    // the `token` represents the transaction
});

Contributing

First, install the dependencies using yarn:

$ yarn install --pure-lockfile

Verify that your project is configured correctly by launching tests:

$ yarn test

Before you start coding make sure that you've read our CONTRIBUTING guide!