onesignal-api-client-core
v1.2.2
Published
Typescript client for OneSignal Server Rest API
Downloads
3,247
Readme
OneSignal API Client
Features
- Written in Typescript.
- The code is covered by tests.
- The library uses Axios.
- Two clients for different method calls: OneSignalAppClient, OneSignalUserClient. OneSignalAppClient is created for notifications, devices, tracks or sessions. OneSignalUserClient is created for apps.
- Support create a notification builder. You may target users in one of three ways using this builders: by NotificationBySegmentBuilder, by NotificationByFilterBuilder, or by NotificationByDeviceBuilder.
- Support methods for different contents: Notification or Email. Support methods is available in the builders.
Getting Started
Npm
npm install onesignal-api-client-core
Yarn
yarn add onesignal-api-client-core
Using
Create notification
The Create Notification method is used when you want your server to programmatically send notifications or emails to a segment or individual users. You may target users in one of three ways using this method: by Segment, by Filter, or by Device. At least one targeting parameter must be specified.
Create notification through the Segment
API Reference Segments are the most common way developers send notifications via OneSignal. Sending to segments is easy: you simply specify which segments you want to send to, and, optionally, which ones you don't.
import { OneSignalAppClient, NotificationBySegmentBuilder } from 'onesignal-api-client-core';
const client = new OneSignalAppClient('appId', 'restApiKey');
const input = new NotificationBySegmentBuilder()
.setIncludedSegments(['Active Users', 'Inactive Users'])
.notification() // .email()
.setContents({ en: 'My Message' })
.build();
const result = await client.createNotification(input);
Create notification through the Filter
API Reference Filters are a powerful way to target users, allowing you to use both data that OneSignal has about a user and any Tags your app may send OneSignal. Filters can be combined together to form advanced, highly precise user targeting. OneSignal customers use all sorts of filters to send notifications, including language, location, user activity, and more.
import { OneSignalAppClient, NotificationByFilterBuilder } from 'onesignal-api-client-core';
const client = new OneSignalAppClient('appId', 'restApiKey');
const filters = [
{ 'field': 'tag', 'key': 'level', 'relation': '>', 'value': '10' },
{ 'field': 'amount_spent', 'relation': '>', 'value': '0' },
];
const input = new NotificationByFilterBuilder()
.setFilters(filters)
.notification() // .email()
.setContents({ en: 'My Message' })
.build();
const result = await client.createNotification(input);
Create notification through the Device
API Reference You may also target specific devices with the create notification method. Targeting devices is typically used in two ways:
- For notifications that target individual users, such as if they've received a message from someone.
- For apps that wish to manage their own segments, such as tracking a user's followers and sending notifications to them when that user posts.
import { OneSignalAppClient, NotificationByDeviceBuilder } from 'onesignal-api-client-core';
const client = new OneSignalAppClient('appId', 'restApiKey');
const input = new NotificationByDeviceBuilder()
.setIncludeExternalUserIds(['externalUserId1', 'externalUserId2'])
.notification() // .email()
.setContents({ en: 'My Message' })
.build();
const result = await client.createNotification(input);
Cancel notification
API Reference Stop a scheduled or currently outgoing notification.
import { OneSignalAppClient, ICancelNotificationInput } from 'onesignal-api-client-core';
const client = new OneSignalAppClient('appId', 'restApiKey');
const input = { id: notification.id } as ICancelNotificationInput;
const result = await client.cancelNotification(input);
Create, View or Update a Device
import { OneSignalAppClient, ICreateDeviceInput, IViewDeviceInput, IUpdateDeviceInput, DeviceType } from 'onesignal-api-client-core';
const client = new OneSignalAppClient('appId', 'restApiKey');
const createInput = { device_type: DeviceType.ChromeWebPush } as ICreateDeviceInput;
const createResult = await client.createDevice(input);
const viewInput = { id: device.id } as IViewDeviceInput;
const viewResult = await client.viewDevice(viewInput);
const viewsResult = await client.viewDevices();
const updateInput = { id: device.id, device_type: DeviceType.Android } as IUpdateDeviceInput;
const updateResult = await client.updateDevice(input);
Create, View or Update an OneSignal App
import { OneSignalUserClient, ICreateAppInput, IViewAppInput, IUpdateAppInput } from 'onesignal-api-client-core';
const client = new OneSignalUserClient('userAuthKey');
const createInput = { name } as ICreateAppInput;
const createResult = await client.createApp(input);
const viewInput = { id: app.id } as IViewAppInput;
const viewResult = await client.viewApp(viewInput);
const viewsResult = await client.viewApps();
const updateInput = { id: app.id, name: 'Updated name' } as IUpdateAppInput;
const updateResult = await client.updateApp(input);
Track open
import { OneSignalAppClient, ITrackOpenInput } from 'onesignal-api-client-core';
const client = new OneSignalAppClient('appId', 'restApiKey');
const trackOpenInput = { notificationId: notification.id, opened: true } as ITrackOpenInput;
const trackOpenResult = await client.trackOpen(input);
New Session
import { OneSignalAppClient, INewSessionInput } from 'onesignal-api-client-core';
const client = new OneSignalAppClient('appId', 'restApiKey');
const newSessionInput = { deviceId: device.id } as INewSessionInput;
const newSessionResult = await client.newSession(input);
OneSignal server implementation methods
List of server api methods.
- [x] Create notification
- [x] Cancel notification
- [x] View apps
- [x] View an app
- [x] Create an app
- [x] Update an app
- [x] View devices
- [x] View device
- [x] Add a device
- [x] Edit device
- [x] New session
- [ ] New purchase
- [ ] Increment session length
- [ ] CSV export
- [x] View notification
- [x] View notifications
- [x] Track open
- [ ] Notification History
- [ ] Create Segments
- [ ] Delete Segments
Support
If any bugs are found in the API wrapper, please open an issue on GitHub, or a Pull Request if you want to fix it yourself! Please be as explicit as possible and provide a minimum reproducing repository if at all possible, as it helps track down what went wrong.
Documentation
All documentation for this wrapper comes from the Server Rest API, if there are any typos, please let me know or open a PR to fix it.
TODO
- Implements all methods
Licence
MIT