GithubHelp home page GithubHelp logo

sourleangchhean168 / onesignal-node Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zeyneloz/onesignal-node

1.0 0.0 0.0 22 KB

A Node.js Library for OneSignal push notification service

License: MIT License

JavaScript 100.00%

onesignal-node's Introduction

onesignal-node

A Node.js client library for OneSignal API.

Table of Contents

Installation

npm install onesignal-node --save

Usage

var OneSignal = require('onesignal-node');

Creating a client

You can create a OneSignal Client as shown below. It takes a JSON object as parameter which contains your OneSignal API credentials. You can find your userAuthKey and REST API Key (appAuthKey) on OneSignal Account & API Keys page.

// create a new Client for a single app
var myClient = new OneSignal.Client({
	userAuthKey: 'XXXXXX',
	// note that "app" must have "appAuthKey" and "appId" keys
	app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

You can also create a Client for multiple Apps

// create a Client for a multiple apps
var myClient = new OneSignal.Client({
	userAuthKey: 'XXXXXX',
	apps: ['id1', 'id2'] // your app ids
});

You can always create a Client with no credential and set them later:

// create a Client for a multiple apps
var myClient = new OneSignal.Client({});
myClient.userAuthKey = 'XXXXXX';

myClient.app = { appAuthKey: 'XXXXX', appId: 'XXXXX' };
// or
myClient.setApp({ appAuthKey: 'XXXXX', appId: 'XXXXX' });

myClient.apps = ['id1', 'id2', 'id3']; // this will override "app"

Creating new notification object

We will pass Notification objects to the Client object to send them.

// contents is REQUIRED unless content_available=true or template_id is set.
var firstNotification = new OneSignal.Notification({
    contents: {
        en: "Test notification",
        tr: "Test mesajı"
    }
});

You can also create a Notification object without contents:

var firstNotification = new OneSignal.Notification({
    content_available: true
});

// or if you want to use template_id instead:
var firstNotification = new OneSignal.Notification({
    template_id: "be4a8044-bbd6-11e4-a581-000c2940e62c"
});

You can set filters, data, buttons and all of the fields available on OneSignal Documentation by using .setParameter(paramName, paramValue) function:

var firstNotification = new OneSignal.Notification({
    contents: {
        en: "Test notification",
        tr: "Test mesajı"
    }
});
firstNotification.setParameter('data', {"abc": "123", "foo": "bar"});
firstNotification.setParameter('headings', {"en": "English Title", "es": "Spanish Title"});

Sending Push Notifications

Sending a notification using Segments:

var OneSignal = require('onesignal-node');

// first we need to create a client
var myClient = new OneSignal.Client({
    userAuthKey: 'XXXXXX',
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

// we need to create a notification to send
var firstNotification = new OneSignal.Notification({
    contents: {
        en: "Test notification",
        tr: "Test mesajı"
    }
});

// set target users
firstNotification.setIncludedSegments(['All']);
firstNotification.setExcludedSegments(['Inactive Users']);

// set notification parameters
firstNotification.setParameter('data', {"abc": "123", "foo": "bar"});
firstNotification.setParameter('send_after', 'Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)');

// send this notification to All Users except Inactive ones
myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
   if (err) {
       console.log('Something went wrong...');
   } else {
       console.log(data, httpResponse.statusCode);
   }
});

You can also use Promises:

myClient.sendNotification(firstNotification)
    .then(function (response) {
        console.log(response.data, response.httpResponse.statusCode);
    })
    .catch(function (err) {
        console.log('Something went wrong...', err);
    });

To send a notification based on filters, use .setFilters(filters) method:

var OneSignal = require('onesignal-node');

var myClient = new OneSignal.Client({
    userAuthKey: 'XXXXXX',
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

var firstNotification = new OneSignal.Notification({
    contents: {
        en: "Test notification",
        tr: "Test mesajı"
    }
});

firstNotification.setFilters([
    {"field": "tag", "key": "level", "relation": ">", "value": "10"},
    {"field": "amount_spent", "relation": ">","value": "0"}
]);

myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
   if (err) {
       console.log('Something went wrong...');
   } else {
       console.log(data);
   }
});

To target one or more device, use .setTargetDevices(include_player_ids) method:

var OneSignal = require('onesignal-node');

var myClient = new OneSignal.Client({
    userAuthKey: 'XXXXXX',
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

var firstNotification = new OneSignal.Notification({
    contents: {
        en: "Test notification",
        tr: "Test mesajı"
    }
});

firstNotification.setTargetDevices(["1dd608f2-c6a1-11e3-851d-000c2940e62c", 
    "2dd608f2-c6a1-11e3-851d-000c2940e62c"]);
    
myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
   if (err) {
       console.log('Something went wrong...');
   } else {
       console.log(data);
   }
});

Note that .sendNotification(notification, callback) function will send the notification to the app specified during the creation of Client object. If you want to send notification to multiple apps, you must set apps array instead, on Client object:

var myClient = new OneSignal.Client({});
myClient.userAuthKey = 'XXXXXX';
myClient.apps = ['id1', 'id2'];

Cancelling a push notification

You can cancel a notification simply by calling .cancel(notificationId, callback) method

// this will cancel the notification for current app (myClient.app)
myClient.cancelNotification('notificationId', function (err, httpResponse, data) {
    if (err) {
        
    }
})

Viewing push notifications

To view all push notifications for an app:

var myClient = new OneSignal.Client({
    userAuthKey: 'XXXXXX',
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

myClient.viewNotifications('limit=30', function (err, httpResponse, data) {
    if (httpResponse.statusCode === 200 && !err) {
        console.log(data);
    }
});

Viewing a push notification

var myClient = new OneSignal.Client({
    userAuthKey: 'XXXXXX',
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

myClient.viewNotification('notificationId', function (err, httpResponse, data) {
    if (httpResponse.statusCode === 200 && !err) {
        console.log(data);
    }
});

Viewing apps

myClient.viewApps(function (err, httpResponse, data) {
    console.log(data[0].name); // print the name of the app
});

you can also view a single app

myClient.viewApp('appId', function (err, httpResponse, data) {
    console.log(data);
});

Creating an app

var OneSignal = require('onesignal-node');

var myClient = new OneSignal.Client({
    userAuthKey: 'XXXXXX'
});

var appBody = {
    name: 'Test App',
    apns_env: 'production',
    gcm_key: 'xxxxx-aaaaa-bbbb'
};

myClient.createApp(appBody, function (err, httpResponse, data) {
    if (httpResponse.statusCode === 200) {
        console.log(data);
    }
});

Updating an app

var OneSignal = require('onesignal-node');

var myClient = new OneSignal.Client({
    userAuthKey: 'XXXXXX',
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

var appBody = {
    name: 'New Test App',
    gcm_key: 'xxxxx-aaaaa-bbbb'
};

myClient.updateApp(appBody, function (err, httpResponse, data) {
    console.log(data);
});

Viewing devices

You can view devices for an app:

var myClient = new OneSignal.Client({
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

// you can set limit and offset (optional) or you can leave it empty
myClient.viewDevices('limit=100&offset=0', function (err, httpResponse, data) {
    console.log(data);
});

Viewing a device

var myClient = new OneSignal.Client({
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

myClient.viewDevice('deviceId', function (err, httpResponse, data) {
    console.log(data);
});

Adding a device

var myClient = new OneSignal.Client({
    userAuthKey: 'XXXXXX',
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

// If you want to add device to current app, don't add app_id in deviceBody
var deviceBody = {
    device_type: 1,
    language: 'tr'
};

myClient.addDevice(deviceBody, function (err, httpResponse, data) {
    ...
});

Editing a device

var myClient = new OneSignal.Client({
    userAuthKey: 'XXXXXX',
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

var deviceBody = {
    device_type: 0,
    language: 'en',
    device_model: 'iPhone5,1'
};

myClient.editDevice('deviceId', deviceBody, function (err, httpResponse, data) {
    ...
});

CSV Export

var myClient = new OneSignal.Client({
    userAuthKey: 'XXXXXX',
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

myClient.csvExport({ extra_fields: ['location'] }, function (err, httpResponse, data) {
...
});

Opening track

var myClient = new OneSignal.Client({
    userAuthKey: 'XXXXXX',
    app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});

myClient.trackOpen('notificationId', { opened: true }, function (err, httpResponse, data) {
...
});

License

This project is under the MIT license.

onesignal-node's People

Contributors

fxgx avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.