From b16887b49f20675c824d4d73324efc2d34409ad7 Mon Sep 17 00:00:00 2001 From: Rodrigo Gomez Palacio Date: Tue, 9 Jul 2024 13:15:14 -0500 Subject: [PATCH] Change sendSelfNotification to use local push Motivation: we are removing support for unauthenticated notif create calls --- express_webpack/index.html | 7 +++++++ src/OneSignal.ts | 30 ++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/express_webpack/index.html b/express_webpack/index.html index 716575777..6be14dbe0 100644 --- a/express_webpack/index.html +++ b/express_webpack/index.html @@ -154,6 +154,12 @@ }) } + function sendSelfNotification() { + OneSignal.push(function() { + OneSignal.sendSelfNotification(); + }); + } + @@ -189,6 +195,7 @@

OneSignal WebSDK Sandbox

+

diff --git a/src/OneSignal.ts b/src/OneSignal.ts index 5e08babe1..4c5a98668 100755 --- a/src/OneSignal.ts +++ b/src/OneSignal.ts @@ -36,6 +36,7 @@ import { awaitOneSignalInitAndSupported, executeCallback, getConsoleStyle, + getPlatformNotificationIcon, isValidEmail, logMethodCall, } from './utils'; @@ -713,16 +714,16 @@ export default class OneSignal { /** * @PublicApi */ - static async sendSelfNotification(title: string = 'OneSignal Test Message', - message: string = 'This is an example notification.', - url: string = `${new URL(location.href).origin}?_osp=do_not_open`, - icon: URL, - data: Map, + static async sendSelfNotification(title = 'OneSignal Test Message', + message = 'This is an example notification.', + url = `${new URL(location.href).origin}?_osp=do_not_open`, + icon: string, + data: Record, buttons: Array): Promise { await awaitOneSignalInitAndSupported(); logMethodCall('sendSelfNotification', title, message, url, icon, data, buttons); const appConfig = await Database.getAppConfig(); - const subscription = await Database.getSubscription(); + if (!appConfig.appId) throw new InvalidStateError(InvalidStateReason.MissingAppId); if (!(await OneSignal.isPushNotificationsEnabled())) @@ -731,11 +732,20 @@ export default class OneSignal { throw new InvalidArgumentError('url', InvalidArgumentReason.Malformed); if (!ValidatorUtils.isValidUrl(icon, { allowEmpty: true, requireHttps: true })) throw new InvalidArgumentError('icon', InvalidArgumentReason.Malformed); - - if (subscription.deviceId) { - await OneSignalApi.sendNotification(appConfig.appId, [subscription.deviceId], { en : title }, { en : message }, - url, icon, data, buttons); + if (!icon) { + // get default icon + const icons = await MainHelper.getNotificationIcons(); + icon = getPlatformNotificationIcon(icons); } + + navigator.serviceWorker.ready.then(async (registration) => { + const options: NotificationOptions = { + body: message, + data: data, + icon: icon, + }; + registration.showNotification(title, options); + }); } /**