|
| 1 | +import { ErrorHandlingService } from '../errors/ErrorHandlingService'; |
| 2 | +// SvelteKit env imports. Public App ID may be used on client; REST key must stay private. |
| 3 | +import { PUBLIC_ONESIGNAL_APP_ID, PUBLIC_ONESIGNAL_SAFARI_WEB_ID } from '$env/static/public'; |
| 4 | +import { PRIVATE_ONESIGNAL_REST_API_KEY } from '$env/static/private'; |
| 5 | + |
| 6 | +/** |
| 7 | + * OneSignal push helper (2025 docs: Create message API) |
| 8 | + * Env vars (SvelteKit style): |
| 9 | + * PUBLIC_ONESIGNAL_APP_ID (public) |
| 10 | + * PUBLIC_ONESIGNAL_SAFARI_WEB_ID (public, optional for Safari web push) |
| 11 | + * PRIVATE_ONESIGNAL_REST_API_KEY (server only) |
| 12 | + */ |
| 13 | +export class OneSignalService { |
| 14 | + constructor(private errorHandler: ErrorHandlingService) {} |
| 15 | + |
| 16 | + private assertEnv() { |
| 17 | + if (!PUBLIC_ONESIGNAL_APP_ID) throw new Error('PUBLIC_ONESIGNAL_APP_ID not configured'); |
| 18 | + if (!PRIVATE_ONESIGNAL_REST_API_KEY) |
| 19 | + throw new Error('PRIVATE_ONESIGNAL_REST_API_KEY not configured'); |
| 20 | + } |
| 21 | + |
| 22 | + private baseHeaders() { |
| 23 | + this.assertEnv(); |
| 24 | + return { |
| 25 | + 'content-type': 'application/json; charset=utf-8', |
| 26 | + authorization: `Key ${PRIVATE_ONESIGNAL_REST_API_KEY}` |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Send a push notification. |
| 32 | + * Provide either externalUserIds (preferred) or includedSegments. |
| 33 | + */ |
| 34 | + async sendPush(options: { |
| 35 | + headings?: Record<string, string>; |
| 36 | + contents: Record<string, string>; |
| 37 | + externalUserIds?: string[]; // OneSignal External IDs |
| 38 | + includedSegments?: string[]; // e.g. ['Test Users'] |
| 39 | + data?: Record<string, unknown>; |
| 40 | + iosAttachments?: Record<string, string>; |
| 41 | + bigPicture?: string; // Android image |
| 42 | + name?: string; // internal name in dashboard |
| 43 | + }): Promise<any> { |
| 44 | + try { |
| 45 | + this.assertEnv(); |
| 46 | + const { |
| 47 | + headings, |
| 48 | + contents, |
| 49 | + externalUserIds, |
| 50 | + includedSegments, |
| 51 | + data, |
| 52 | + iosAttachments, |
| 53 | + bigPicture, |
| 54 | + name |
| 55 | + } = options; |
| 56 | + |
| 57 | + if (!contents || Object.keys(contents).length === 0) { |
| 58 | + throw new Error('contents is required'); |
| 59 | + } |
| 60 | + if (!externalUserIds?.length && !includedSegments?.length) { |
| 61 | + throw new Error('Must supply externalUserIds or includedSegments'); |
| 62 | + } |
| 63 | + |
| 64 | + const body: any = { |
| 65 | + app_id: PUBLIC_ONESIGNAL_APP_ID, |
| 66 | + target_channel: 'push', |
| 67 | + contents, |
| 68 | + headings, |
| 69 | + data, |
| 70 | + name |
| 71 | + }; |
| 72 | + if (externalUserIds?.length) body.include_external_user_ids = externalUserIds; |
| 73 | + if (includedSegments?.length) body.included_segments = includedSegments; |
| 74 | + if (iosAttachments) body.ios_attachments = iosAttachments; |
| 75 | + if (bigPicture) body.big_picture = bigPicture; |
| 76 | + |
| 77 | + const res = await fetch('https://api.onesignal.com/notifications', { |
| 78 | + method: 'POST', |
| 79 | + headers: this.baseHeaders(), |
| 80 | + body: JSON.stringify(body) |
| 81 | + }); |
| 82 | + if (!res.ok) { |
| 83 | + const text = await res.text(); |
| 84 | + throw new Error(`OneSignal error ${res.status}: ${text}`); |
| 85 | + } |
| 86 | + return await res.json(); |
| 87 | + } catch (err) { |
| 88 | + this.errorHandler.logError(err as Error); |
| 89 | + throw err; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Optional helper for web push initialization (Safari ID is exposed for completeness) |
| 95 | +export const ONE_SIGNAL_PUBLIC_CONFIG = { |
| 96 | + appId: PUBLIC_ONESIGNAL_APP_ID, |
| 97 | + safari_web_id: PUBLIC_ONESIGNAL_SAFARI_WEB_ID |
| 98 | +}; |
0 commit comments