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.
$ npm i nestjs-node-onesignal
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 { }
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
This package is MIT licensed.