@@ -24,6 +24,9 @@ export default class Notifications {
2424 private _permissionObserverList : ( ( event : boolean ) => void ) [ ] = [ ] ;
2525 private _notificationClickedListeners : ( ( event : NotificationClickEvent ) => void ) [ ] = [ ] ;
2626 private _notificationWillDisplayListeners : ( ( event : NotificationWillDisplayEvent ) => void ) [ ] = [ ] ;
27+ private _hasRegisteredClickListener = false ;
28+ private _hasRegisteredForegroundWillDisplayListener = false ;
29+ private _hasRegisteredPermissionListener = false ;
2730
2831 constructor ( plugin : OneSignalCapacitorPlugin ) {
2932 this . _plugin = plugin ;
@@ -103,29 +106,38 @@ export default class Notifications {
103106 ) : void {
104107 if ( event === 'click' ) {
105108 this . _notificationClickedListeners . push ( listener as ( event : NotificationClickEvent ) => void ) ;
106- void this . _plugin . addListener ( 'notificationClick' , ( json : NotificationClickEvent ) => {
107- this . _processFunctionList ( this . _notificationClickedListeners , json ) ;
108- } ) ;
109+ if ( ! this . _hasRegisteredClickListener ) {
110+ this . _hasRegisteredClickListener = true ;
111+ void this . _plugin . addListener ( 'notificationClick' , ( json : NotificationClickEvent ) => {
112+ this . _processFunctionList ( this . _notificationClickedListeners , json ) ;
113+ } ) ;
114+ }
109115 } else if ( event === 'foregroundWillDisplay' ) {
110116 this . _notificationWillDisplayListeners . push (
111117 listener as ( event : NotificationWillDisplayEvent ) => void ,
112118 ) ;
113- void this . _plugin . addListener (
114- 'notificationForegroundWillDisplay' ,
115- ( notification : OSNotification ) => {
116- this . _notificationWillDisplayListeners . forEach ( ( listener ) => {
117- listener ( new NotificationWillDisplayEvent ( notification ) ) ;
118- } ) ;
119- void this . _plugin . proceedWithWillDisplay ( {
120- notificationId : notification . notificationId ,
121- } ) ;
122- } ,
123- ) ;
119+ if ( ! this . _hasRegisteredForegroundWillDisplayListener ) {
120+ this . _hasRegisteredForegroundWillDisplayListener = true ;
121+ void this . _plugin . addListener (
122+ 'notificationForegroundWillDisplay' ,
123+ ( notification : OSNotification ) => {
124+ this . _notificationWillDisplayListeners . forEach ( ( listener ) => {
125+ listener ( new NotificationWillDisplayEvent ( notification ) ) ;
126+ } ) ;
127+ void this . _plugin . proceedWithWillDisplay ( {
128+ notificationId : notification . notificationId ,
129+ } ) ;
130+ } ,
131+ ) ;
132+ }
124133 } else if ( event === 'permissionChange' ) {
125134 this . _permissionObserverList . push ( listener as ( event : boolean ) => void ) ;
126- void this . _plugin . addListener ( 'permissionChange' , ( state : { permission : boolean } ) => {
127- this . _processFunctionList ( this . _permissionObserverList , state . permission ) ;
128- } ) ;
135+ if ( ! this . _hasRegisteredPermissionListener ) {
136+ this . _hasRegisteredPermissionListener = true ;
137+ void this . _plugin . addListener ( 'permissionChange' , ( state : { permission : boolean } ) => {
138+ this . _processFunctionList ( this . _permissionObserverList , state . permission ) ;
139+ } ) ;
140+ }
129141 }
130142 }
131143
0 commit comments