Skip to content

๐Ÿ’‰ Injectable OneSignal client for NestJS

License

Notifications You must be signed in to change notification settings

mughieams/nestjs-onesignal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

nestjs-onesignal

Nest Logo

๐Ÿ’‰ Injectable OneSignal client for NestJS.

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