mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:04:53 +00:00
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
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');
|
|
}
|
|
}
|
|
}
|