@@ -12,8 +12,97 @@ import Utils from '../context/Utils';
12
12
import Database from '../services/Database' ;
13
13
import { PermissionUtils } from '../utils/PermissionUtils' ;
14
14
import Environment from './Environment' ;
15
+ import { getPlatformNotificationIcon , logMethodCall } from '../utils/utils' ;
16
+ import {
17
+ NotSubscribedError ,
18
+ NotSubscribedReason ,
19
+ } from '../errors/NotSubscribedError' ;
20
+ import {
21
+ InvalidArgumentError ,
22
+ InvalidArgumentReason ,
23
+ } from '../errors/InvalidArgumentError' ;
24
+ import { ValidatorUtils } from '../../page/utils/ValidatorUtils' ;
15
25
16
26
export default class MainHelper {
27
+ static async showLocalNotification (
28
+ title : string ,
29
+ message : string ,
30
+ url : string ,
31
+ icon ?: string ,
32
+ data ?: Record < string , any > ,
33
+ buttons ?: Array < any > ,
34
+ ) : Promise < void > {
35
+ logMethodCall (
36
+ 'MainHelper:showLocalNotification: ' ,
37
+ title ,
38
+ message ,
39
+ url ,
40
+ icon ,
41
+ data ,
42
+ buttons ,
43
+ ) ;
44
+
45
+ const appConfig = await Database . getAppConfig ( ) ;
46
+
47
+ if ( ! appConfig . appId )
48
+ throw new InvalidStateError ( InvalidStateReason . MissingAppId ) ;
49
+ if ( ! OneSignal . Notifications . permission )
50
+ throw new NotSubscribedError ( NotSubscribedReason . NoDeviceId ) ;
51
+ if ( ! ValidatorUtils . isValidUrl ( url ) )
52
+ throw new InvalidArgumentError ( 'url' , InvalidArgumentReason . Malformed ) ;
53
+ if (
54
+ ! ValidatorUtils . isValidUrl ( icon , { allowEmpty : true , requireHttps : true } )
55
+ )
56
+ throw new InvalidArgumentError ( 'icon' , InvalidArgumentReason . Malformed ) ;
57
+ if ( ! icon ) {
58
+ // get default icon
59
+ const icons = await MainHelper . getNotificationIcons ( ) ;
60
+ icon = getPlatformNotificationIcon ( icons ) ;
61
+ }
62
+
63
+ const convertButtonsToNotificationActionType = ( buttons : Array < any > ) => {
64
+ const convertedButtons = [ ] ;
65
+
66
+ for ( let i = 0 ; i < buttons . length ; i ++ ) {
67
+ const button = buttons [ i ] ;
68
+ convertedButtons . push ( {
69
+ action : button . id ,
70
+ title : button . text ,
71
+ icon : button . icon ,
72
+ url : button . url ,
73
+ } ) ;
74
+ }
75
+
76
+ return convertedButtons ;
77
+ } ;
78
+ const dataPayload = {
79
+ data,
80
+ url,
81
+ buttons : buttons
82
+ ? convertButtonsToNotificationActionType ( buttons )
83
+ : undefined ,
84
+ } ;
85
+
86
+ OneSignal . context . serviceWorkerManager
87
+ . getRegistration ( )
88
+ . then ( async ( registration ?: ServiceWorkerRegistration | null ) => {
89
+ if ( ! registration ) {
90
+ Log . error ( 'Service worker registration not available.' ) ;
91
+ return ;
92
+ }
93
+
94
+ const options = {
95
+ body : message ,
96
+ data : dataPayload ,
97
+ icon : icon ,
98
+ actions : buttons
99
+ ? convertButtonsToNotificationActionType ( buttons )
100
+ : [ ] ,
101
+ } ;
102
+ registration . showNotification ( title , options ) ;
103
+ } ) ;
104
+ }
105
+
17
106
static async checkAndTriggerNotificationPermissionChanged ( ) {
18
107
const previousPermission = await Database . get (
19
108
'Options' ,
0 commit comments