mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
Add OneSignal configuration and service
This commit is contained in:
9
libs/common/src/config/onesignal.config.ts
Normal file
9
libs/common/src/config/onesignal.config.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { registerAs } from '@nestjs/config';
|
||||
|
||||
export default registerAs(
|
||||
'onesignal-config',
|
||||
(): Record<string, any> => ({
|
||||
ONESIGNAL_APP_ID: process.env.ONESIGNAL_APP_ID,
|
||||
ONESIGNAL_API_KEY: process.env.ONESIGNAL_API_KEY,
|
||||
}),
|
||||
);
|
41
libs/common/src/helper/services/onesignal.service.ts
Normal file
41
libs/common/src/helper/services/onesignal.service.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import * as OneSignal from 'onesignal-node';
|
||||
|
||||
@Injectable()
|
||||
export class OneSignalService {
|
||||
private client: any;
|
||||
|
||||
constructor(private readonly configService: ConfigService) {
|
||||
// Initialize OneSignal client here
|
||||
this.client = new OneSignal.Client(
|
||||
this.configService.get<string>('onesignal-config.ONESIGNAL_APP_ID'),
|
||||
this.configService.get<string>('onesignal-config.ONESIGNAL_API_KEY'),
|
||||
);
|
||||
}
|
||||
|
||||
async sendNotification(
|
||||
content: string,
|
||||
title: string,
|
||||
subscriptionIds: string[],
|
||||
): Promise<any> {
|
||||
const notification = {
|
||||
contents: {
|
||||
en: content,
|
||||
},
|
||||
headings: {
|
||||
en: title,
|
||||
},
|
||||
include_subscription_ids: subscriptionIds,
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await this.client.createNotification(notification);
|
||||
|
||||
return response;
|
||||
} catch (err) {
|
||||
console.error('Error:', err);
|
||||
throw new Error('Error sending notification');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user