Skip to content

NestJS wrapper for OneSignal official package: @onesignal/node-onesignal

License

Notifications You must be signed in to change notification settings

PazminoJose/nestjs-node-onesignal

Repository files navigation

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.

About

NestJS wrapper for OneSignal official package: @onesignal/node-onesignal

Resources

License

Stars

Watchers

Forks

Packages

No packages published