Package Exports
- nestjs-node-onesignal
- nestjs-node-onesignal/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nestjs-node-onesignal) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
NestJS wrapper for OneSignal official package: @onesignal/node-onesignal
[!warning] This package is based on @onesignal/node-onesignal which is currently in alpha state so it may come with breaking changes in the future, use with caution.
Installation
$ npm i nestjs-node-onesignal
Getting Started
To use OneSignal client you need register OneSignalModule in your app for example in app.module.ts
import { OneSignalModule } from 'nestjs-node-onesignal';
@Module({
imports: [
OneSignalModule.forRoot({
appId: process.env.ONESIGNAL_APP_ID,
restApiKey: process.env.ONESIGNAL_API_KEY,
})
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
If you are using the ConfigModule from the Nest package @nestjs/config, you can use the registerAsync() function to inject your environment variables like this:
import { OneSignalModule } from 'nestjs-node-onesignal';
@Module({
imports: [
OneSignalModule.forRootAsync({
useFactory: async (configService: ConfigService) => ({
appId: configService.get<string>("ONESIGNAL_APP_ID"),
restApiKey: configService.get<string>("ONESIGNAL_API_KEY")
}),
inject: [ConfigService]
})
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
Example
The One signal service comes with an integrated notification builder so you can easily create your notifications.
import { OneSignalService } from "nestjs-node-onesignal";
@Injectable()
export class AppService {
constructor(
private readonly oneSignalService: OneSignalService,
) {}
async sendNotification() {
const playerId = this.configService.get(ONESIGNAL_PLAYER_ID);
const imageUrl = "https://www.example.com/image.jpg";
const notification = this.oneSignalService.notificationBuilder
.setContents({
en: 'Send notification to a specific player ID',
})
.setIncludePlayerIds([playerId])
.setContentAvailable(true)
.setBigPicture(imageUrl)
.setIosAttachments({ id1: this.imageUrl })
.build();
return await this.oneSignalService.client.createNotification(notification);
}
}
For full api reference see One Signal Node SDK
License
This package is MIT licensed.