Skip to content

Commit

Permalink
Change sendSelfNotification to use local push
Browse files Browse the repository at this point in the history
Motivation: we are removing support for unauthenticated notif create calls
  • Loading branch information
rgomezp committed Jul 10, 2024
1 parent eb73b54 commit b16887b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
7 changes: 7 additions & 0 deletions express_webpack/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@
})
}

function sendSelfNotification() {
OneSignal.push(function() {
OneSignal.sendSelfNotification();
});
}

</script>
<head>
<meta charset="utf-8">
Expand Down Expand Up @@ -189,6 +195,7 @@ <h1>OneSignal WebSDK Sandbox</h1>
<button onclick="javascript:showSmsSlidedown();">Show Sms Slidedown</button>
<button onclick="javascript:showEmailSlidedown();">Show Email Slidedown</button>
<button onclick="javascript:showSmsAndEmailSlidedown();">Show Sms & Email Slidedown</button>
<button onclick="javascript:sendSelfNotification();">Send Self Notification</button>
<br />
<br />
<div class='onesignal-customlink-container'></div>
Expand Down
30 changes: 20 additions & 10 deletions src/OneSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
awaitOneSignalInitAndSupported,
executeCallback,
getConsoleStyle,
getPlatformNotificationIcon,
isValidEmail,
logMethodCall,
} from './utils';
Expand Down Expand Up @@ -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<String, any>,
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<string, any>,
buttons: Array<NotificationActionButton>): Promise<void> {
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()))
Expand All @@ -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);
});
}

/**
Expand Down

0 comments on commit b16887b

Please sign in to comment.