diff --git a/express_webpack/index.html b/express_webpack/index.html index 716575777..6919a6d73 100644 --- a/express_webpack/index.html +++ b/express_webpack/index.html @@ -154,6 +154,17 @@ }) } + function sendSelfNotification() { + OneSignal.push(function() { + OneSignal.sendSelfNotification('Title', 'message', `https://localhost:4001/?app_id=${appId}`, 'https://t3.ftcdn.net/jpg/03/08/73/34/360_F_308733458_QBzwMVu8ZzdGEp9Wwq1fAYaDgtP3UVwl.jpg', { test: 'foo' }, [{ + id: 'like-button', + text: 'Like', + icon: 'https://image.similarpng.com/very-thumbnail/2020/06/Icon-like-button-transparent-PNG.png', + url: 'https://onesignal.com' + }]); + }); + } + @@ -189,6 +200,7 @@

OneSignal WebSDK Sandbox

+

diff --git a/package.json b/package.json index 98cdaa207..19d15c704 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "jest": "jest --coverage" }, "config": { - "sdkVersion": "151605" + "sdkVersion": "151606" }, "repository": { "type": "git", diff --git a/src/OneSignal.ts b/src/OneSignal.ts index 5e08babe1..5be5c778a 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, - buttons: Array): Promise { + 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,48 @@ 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 (!icon) { + // get default icon + const icons = await MainHelper.getNotificationIcons(); + icon = getPlatformNotificationIcon(icons); + } + + const convertButtonsToNotificationActionType = (buttons: Array) => { + const convertedButtons = []; - if (subscription.deviceId) { - await OneSignalApi.sendNotification(appConfig.appId, [subscription.deviceId], { en : title }, { en : message }, - url, icon, data, buttons); + for (let i=0; i { + if (!registration) { + Log.error("Service worker registration not available."); + return; + } + + const options = { + body: message, + data: dataPayload, + icon: icon, + actions: buttons ? convertButtonsToNotificationActionType(buttons) : [], + }; + registration.showNotification(title, options); + }); } /** diff --git a/src/OneSignalApi.ts b/src/OneSignalApi.ts index 9edff1ca1..f3b2d793f 100755 --- a/src/OneSignalApi.ts +++ b/src/OneSignalApi.ts @@ -17,10 +17,6 @@ export default class OneSignalApi { return OneSignalApiShared.updatePlayer(appId, playerId, options); } - static sendNotification(appId: string, playerIds: Array, titles, contents, url, icon, data, buttons) { - return OneSignalApiShared.sendNotification(appId, playerIds, titles, contents, url, icon, data, buttons); - } - static jsonpLib(url: string, fn: Function) { JSONP(url, null, fn); } diff --git a/src/OneSignalApiShared.ts b/src/OneSignalApiShared.ts index df0b27916..a2beb2d36 100644 --- a/src/OneSignalApiShared.ts +++ b/src/OneSignalApiShared.ts @@ -21,29 +21,6 @@ export default class OneSignalApiShared { return OneSignalApiBase.put(`players/${playerId}`, { app_id: appId, ...options }); } - static sendNotification(appId: string, playerIds: Array, titles, contents, url, icon, data, buttons) { - var params = { - app_id: appId, - contents: contents, - include_player_ids: playerIds, - isAnyWeb: true, - data: data, - web_buttons: buttons - }; - if (titles) { - (params as any).headings = titles; - } - if (url) { - (params as any).url = url; - } - if (icon) { - (params as any).chrome_web_icon = icon; - (params as any).firefox_icon = icon; - } - Utils.trimUndefined(params); - return OneSignalApiBase.post('notifications', params); - } - static async createUser(deviceRecord: DeviceRecord): Promise { const serializedDeviceRecord = deviceRecord.serialize(); Utils.enforceAppId(serializedDeviceRecord.app_id); diff --git a/src/helpers/EventHelper.ts b/src/helpers/EventHelper.ts index eb7f3d91b..6b023c1df 100755 --- a/src/helpers/EventHelper.ts +++ b/src/helpers/EventHelper.ts @@ -1,6 +1,5 @@ import Event from '../Event'; import LimitStore from '../LimitStore'; -import OneSignalApiShared from '../OneSignalApiShared'; import Database from '../services/Database'; import { ContextSWInterface } from "../models/ContextSW"; import Log from '../libraries/Log'; @@ -84,8 +83,6 @@ export default class EventHelper { } EventHelper.sendingOrSentWelcomeNotification = true; - const { deviceId } = await Database.getSubscription(); - const { appId } = await Database.getAppConfig(); let title = welcome_notification_opts !== undefined && welcome_notification_opts['title'] !== undefined && @@ -108,13 +105,11 @@ export default class EventHelper { message = BrowserUtils.decodeHtmlEntities(message); Log.debug('Sending welcome notification.'); - OneSignalApiShared.sendNotification( - appId, - [deviceId], - { en: title }, - { en: message }, + OneSignal.sendSelfNotification( + title, + message, url, - null, + undefined, { __isOneSignalWelcomeNotification: true }, undefined );