2.0.0 • Published 2 years ago

nestjs-onesignal v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

codecov npm version miniziped size MIT licensed

Implementing the OneSignalModule from this package you gain access to OneSignal client through dependency injection with minimal setup.

Instalation

$ npm install --save @onesignal/node-onesignal nestjs-onesignal
$ yarn add @onesignal/node-onesignal nestjs-onesignal

Getting Started

To use OneSignal client we need to register module for example in app.module.ts

import { OneSignalModule } from 'nestjs-onesignal';

@Module({
    imports: [
        OneSignalModule.forRoot({
            appId: process.env.ONESIGNAL_APP_ID,
            apiKey: process.env.ONESIGNAL_API_KEY,
        }),
    ],
})
export class AppModule {}

If you are using the @nestjs/config package from Nest, you can use the ConfigModule using the registerAsync() function to inject your environment variables like this in your custom module:

import { OneSignalModule } from 'nestjs-onesignal';

@Module({
    imports: [
        OneSignalModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: (configService: ConfigService) => ({
                appId: configService.get('ONESIGNAL_APP_ID'),
                apiKey: configService.get('ONESIGNAL_API_KEY'),
            }),
            inject: [ConfigService],
        }),
    ],
})
export class AppModule {}

Example usage in service.

import { OneSignalService } from 'nestjs-twilio';

@Injectable()
export class AppService {
    public constructor(
        private readonly onesignalService: OneSignalService,
        private readonly configService: ConfigService,
    ) {}
    
    async sendNotification() {
        const playerId = this.configService.get(ONESIGNAL_PLAYER_ID);
        
        return await this.onesignalService.client.createNotification({
            contents: {
                en: 'Sent notification to spesific player id',
            },
            include_player_ids: [playerId],
        });
    }
}

For full Client Library see One Signal Node SDK reference here