This sample app demonstrates how to send push and email notifications to your app users using the OneSignal NodeJS API Library and AWS Lambdas.
The NodeJS code was generated with NodeJS version v14.16.0
Take a look at the OneSignal documentation to learn how to integrate OneSignal into your project. After you have integrated OneSignal into your application, you can use NodeJS to send push notification using the OneSignal NodeJS API Library.
The AWS Lambda was generated with AWS Lambda NodeJS version v16.X.X.
-
Install the OneSignal NodeJS API library by running
npm install @onesignal/node-onesignal --save
(npm install). -
Now, create the .zip from the project to be able to upload it to AWS Lambda.
Run the following command to create the .zip file:
-
Windows:
7z a -tzip -r OneSignal-NodeJS-AWS-Lambda-Sample.zip .
-
OSX:
zip -r OneSignal-NodeJS-AWS-Lambda-Sample.zip .
Upload the .zip file in your AWS Lambda console.
Note: You will have to create enviroment variables to run the AWS Lambda (Configuration->Enviroment Variables).
Keys for the enviroment variables:
- ONESIGNAL_APP_ID: The OneSignal App ID.
- REST_API_KEY: The OneSignal API Key.
To authentificate the app, you will need to use your OneSignal REST API key. Create a key provider object with the function getToken()
that returns your key.
const app_key_provider = {
getToken() {
return process.env.REST_API_KEY;
}
};
We can configure the client using the createConfiguration()
function. The configuration object can be used to set the app_key_provider
properties.
const configuration = OneSignal.createConfiguration({
authMethods: {
user_key: {
tokenProvider: user_key_provider
},
app_key: {
tokenProvider: app_key_provider
}
}
});
const client = new OneSignal.DefaultApi(configuration);
Create a push notification and send it to your users of your app.
const notification = new OneSignal.Notification();
notification.app_id = ONESIGNAL_APP_ID;
notification.included_segments = ['Subscribed Users'];
notification.contents = {
en: "Hello OneSignal!"
};
const {id} = await client.createNotification(notification);
Create a email notification and send it to your users of your app.
Note: You will need to setup an email provider to send emails.
const email = new OneSignal.Notification();
email.app_id = ONESIGNAL_APP_ID;
email.included_segments = ['Subscribed Users'];
email.contents.email_subject = 'Welcome to OneSignal!',
email.contents.email_body = '<html><head><body><h1>Welcome to OneSignal NodeJS API Library<h1></body></html>';
await client.createNotification(email);
View the details from a push notification you have sent using OneSignal.
const response = await client.getNotification(ONESIGNAL_APP_ID, id);
console.log(response);
Give a ⭐️ if this project helped you!
The OneSignal Developer community is a group of passionate individuals who work with OneSignal products. Community members have the opportunity to expand their network and knowledge across different technologies.
- Website: https://onesignal.com/onesignal-developers
- Twitter: @OneSignalDevs
- Github: @OneSignalDevelopers
- Discord: @onesignal-metabase