@@ -36,6 +36,7 @@ import {
3636 awaitOneSignalInitAndSupported ,
3737 executeCallback ,
3838 getConsoleStyle ,
39+ getPlatformNotificationIcon ,
3940 isValidEmail ,
4041 logMethodCall ,
4142} from './utils' ;
@@ -713,16 +714,16 @@ export default class OneSignal {
713714 /**
714715 * @PublicApi
715716 */
716- static async sendSelfNotification ( title : string = 'OneSignal Test Message' ,
717- message : string = 'This is an example notification.' ,
718- url : string = `${ new URL ( location . href ) . origin } ?_osp=do_not_open` ,
719- icon : URL ,
720- data : Map < String , any > ,
721- buttons : Array < NotificationActionButton > ) : Promise < void > {
717+ static async sendSelfNotification ( title = 'OneSignal Test Message' ,
718+ message = 'This is an example notification.' ,
719+ url = `${ new URL ( location . href ) . origin } ?_osp=do_not_open` ,
720+ icon ?: string ,
721+ data ?: Record < string , any > ,
722+ buttons ? : Array < any > ) : Promise < void > {
722723 await awaitOneSignalInitAndSupported ( ) ;
723724 logMethodCall ( 'sendSelfNotification' , title , message , url , icon , data , buttons ) ;
724725 const appConfig = await Database . getAppConfig ( ) ;
725- const subscription = await Database . getSubscription ( ) ;
726+
726727 if ( ! appConfig . appId )
727728 throw new InvalidStateError ( InvalidStateReason . MissingAppId ) ;
728729 if ( ! ( await OneSignal . isPushNotificationsEnabled ( ) ) )
@@ -731,11 +732,48 @@ export default class OneSignal {
731732 throw new InvalidArgumentError ( 'url' , InvalidArgumentReason . Malformed ) ;
732733 if ( ! ValidatorUtils . isValidUrl ( icon , { allowEmpty : true , requireHttps : true } ) )
733734 throw new InvalidArgumentError ( 'icon' , InvalidArgumentReason . Malformed ) ;
735+ if ( ! icon ) {
736+ // get default icon
737+ const icons = await MainHelper . getNotificationIcons ( ) ;
738+ icon = getPlatformNotificationIcon ( icons ) ;
739+ }
740+
741+ const convertButtonsToNotificationActionType = ( buttons : Array < any > ) => {
742+ const convertedButtons = [ ] ;
734743
735- if ( subscription . deviceId ) {
736- await OneSignalApi . sendNotification ( appConfig . appId , [ subscription . deviceId ] , { en : title } , { en : message } ,
737- url , icon , data , buttons ) ;
744+ for ( let i = 0 ; i < buttons . length ; i ++ ) {
745+ const button = buttons [ i ] ;
746+ convertedButtons . push ( {
747+ action : button . id ,
748+ title : button . text ,
749+ icon : button . icon ,
750+ url : button . url
751+ } ) ;
752+ }
753+
754+ return convertedButtons ;
755+ } ;
756+
757+ const dataPayload = {
758+ data,
759+ url,
760+ buttons : buttons ? convertButtonsToNotificationActionType ( buttons ) : undefined
738761 }
762+
763+ this . context . serviceWorkerManager . getRegistration ( ) . then ( async ( registration ) => {
764+ if ( ! registration ) {
765+ Log . error ( "Service worker registration not available." ) ;
766+ return ;
767+ }
768+
769+ const options = {
770+ body : message ,
771+ data : dataPayload ,
772+ icon : icon ,
773+ actions : buttons ? convertButtonsToNotificationActionType ( buttons ) : [ ] ,
774+ } ;
775+ registration . showNotification ( title , options ) ;
776+ } ) ;
739777 }
740778
741779 /**
0 commit comments