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

@mehmetcavus/onesignal-node

v3.1.2

Published

A Node.js Library for OneSignal push notification service

Downloads

7

Readme

onesignal-node

A Node.js client library for OneSignal API.
IMPORTANT: This documentation belongs to v3.x.x which has no backward compatibility. Please see this page for v2.x.x.

Table of Contents

Overview

This is a wrapper library over OneSignal REST API. You can create notifications, view apps, edit a device and all other actions you can take on OneSignal REST API.

Installation

npm install onesignal-node --save      

Usage

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

Client Types:

For all the actions that require your OneSignal app id and app api key, you should use OneSignal.Client. Sample actions: create notification, add device, csv export, create segment...

const client = new OneSignal.Client('appId', 'apiKey');

For all the actions that require your User Auth Key you should use OneSignal.UserClient. Sample actions: view apps, update an app, create an app...

const userClient = new OneSignal.UserClient('userAuthKey');

Creating client

You can create a Client object as shown below. It requires two parameters: appId and apiKey, which you can find them on your applications page on OneSignal dashboard.

There is also an optional parameter called options. You can set OneSignal rest api endpoint if you wish to using options. By default the library uses https://onesignal.com/api/v1 for api endpoint.

// With default options
const client = new OneSignal.Client('appId', 'apiKey');

// With custom API endpoint
const client = new OneSignal.Client('appId', 'apiKey', { apiRoot: 'https://onesignal.com/api/v2'});

Creating UserClient

You can create a UserClient object as shown below. It requires one parameter: userAuthKey, which you can find it on your OneSignal dashboard.

There is also an optional parameter called options. You can set OneSignal rest api endpoint if you wish to using options. By default the library uses https://onesignal.com/api/v1 for api endpoint.

// With default options
const userClient = new OneSignal.UserClient('userAuthKey');

// With custom API endpoint
const userClient = new OneSignal.UserClient('userAuthKey', { apiRoot: 'https://onesignal.com/api/v2'});

Create notification

https://documentation.onesignal.com/reference#create-notification

.createNotification(body: CreateNotificationBody): Promise<ClientResponse>

Please read the sections above to learn how to create a Client object.

// See all fields: https://documentation.onesignal.com/reference#create-notification
const notification = {
  contents: {
    'tr': 'Yeni bildirim',
    'en': 'New notification',
  },
  included_segments: ['Subscribed Users'],
  filters: [
    { field: 'tag', key: 'level', relation: '>', value: 10 }
  ]
};

// using async/await
try {
  const response = await client.createNotification(notification);
  console.log(response.body.id);
} catch (e) {
  if (e instanceof OneSignal.HTTPError) {
    // When status code of HTTP response is not 2xx, HTTPError is thrown.
    console.log(e.statusCode);
    console.log(e.body);
  }
}

// or you can use promise style:
client.createNotification(notification)
  .then(response => {})
  .catch(e => {});

Cancel notification

https://documentation.onesignal.com/reference#cancel-notification

.cancelNotification(notificationId: string): Promise<ClientResponse>
const response = await client.cancelNotification('notification-id');
console.log(response.body);
console.log(response.headers);
console.log(response.statusCode);    

View notifications

https://documentation.onesignal.com/reference#view-notifications

.viewNotifications(query?: ViewNotificationsQuery): Promise<ClientResponse>
// without query
const response = await client.viewNotifications();
console.log(response.body);

// with query
const response = await client.viewNotifications({ limit:10, kind: 2, offset: 2 });

View notification

https://documentation.onesignal.com/reference#view-notification

.viewNotification(notificationId: string): Promise<ClientResponse>
const response = await client.viewNotification('notification-id');
console.log(response.body);  

View apps

https://documentation.onesignal.com/reference#view-apps-apps

You should use UserClient for view apps since it requires User Auth Key

.viewApps(): Promise<ClientResponse>
const response = await userClient.viewApps();
console.log(response.body);

Create an app

https://documentation.onesignal.com/reference#create-an-app

You should use UserClient for view apps since it requires User Auth Key

.createApp(body: CreateAppBody): Promise<ClientResponse>
const response = await userClient.createApp( { name: 'APP 1' });
console.log(response.body);

Update an app

https://documentation.onesignal.com/reference#update-an-app

You should use UserClient for view apps since it requires User Auth Key

.updateApp(appId: string, body: UpdateAppBody): Promise<ClientResponse>
const response = await userClient.updateApp( 'app-id',{ site_name: 'test' });
console.log(response.body);

View devices

https://documentation.onesignal.com/reference#view-devices

.viewDevices(query?: LimitOffsetQuery): Promise<ClientResponse>
const response = await client.viewDevices({ limit: 200, offset: 0 });
console.log(response.body);

View device

https://documentation.onesignal.com/reference#view-device

.viewDevice(identifier: string): Promise<ClientResponse>
const response = await client.viewDevice('device-id');
console.log(response.body);

Add a device

https://documentation.onesignal.com/reference#add-a-device

.addDevice(body: AddDeviceBody): Promise<ClientResponse>
const response = await client.addDevice({
  device_type: 'ios',
  identifier: 'id1',
});
console.log(response.body);

Edit a device

https://documentation.onesignal.com/reference#edit-device

.editDevice(deviceId: string, body: EditDeviceBody): Promise<ClientResponse>
const response = await client.editDevice('device-id',{
  identifier: 'id2',
});
console.log(response.body);

New Session

https://documentation.onesignal.com/reference#new-session

.newSession(deviceId: string, body: NewSessionBody): Promise<ClientResponse>
const response = await client.newSession('device-id',{
  language: 'tr',
});
console.log(response.body);

New Purchase

https://documentation.onesignal.com/reference#new-purchase

.newPurchase(deviceId: string, body: NewPurchaseBody): Promise<ClientResponse>
const response = await client.newPurchase('device-id',{
  purchases: [...],
});
console.log(response.body);

Increment Session Length

https://documentation.onesignal.com/reference#increment-session-length

.incrementSessionLength(deviceId: string, body: IncrementSessionLengthBody): Promise<ClientResponse>
const response = await client.incrementSessionLength('device-id',{
  state: '',
  active_time: 11,
});
console.log(response.body);

CSV Export

https://documentation.onesignal.com/reference#csv-export

.exportCSV(body: ExportCSVBody): Promise<ClientResponse>
const response = await client.exportCSV({});
console.log(response.body);

Create Segment

https://documentation.onesignal.com/reference#create-segments

.createSegment(body: CreateSegmentBody): Promise<ClientResponse>
const response = await client.createSegment({
  name: 'new-segment',
  filters: [..]
});
console.log(response.body);

Delete Segment

https://documentation.onesignal.com/reference#delete-segments

.deleteSegment(segmentId: string): Promise<ClientResponse>
const response = await client.deleteSegment('segment-id1');
console.log(response.body);

Tests

Running all tests, coverage, build:

$ npm run test      
$ npm run test-integration      
$ npm run coverage      
$ npm run build      

Contributing

TL;DR

To send a pull request:

  • Fork the repo
  • Clone the forked repo on your computer
  • Switch to master branch
  • Create a feature branch (git checkout -b feature/new-feature-name)
  • Make your changes and add new tests if necessary
  • Push your changes to your fork
  • Open a pull request

License

This project is under the MIT license.