@@ -19,28 +19,25 @@ function ensureCallback() {
1919 public onError ( error : any ) : void { }
2020
2121 public onSuccess ( message : string ) : void {
22- const exec = ( ) => {
23- const callback = this . _owner ?. get ?.( ) ?. [ this . _propName ] ;
24- if ( typeof callback === 'function' ) {
25- if ( this . _propName === '_onToken' ) {
26- callback ( message ) ;
27- } else if ( this . _propName === '_onNotificationTap' || this . _propName === '_onMessage' ) {
28- try {
29- setTimeout ( ( ) => {
30- callback ( JSON . parse ( message ) ) ;
31- } ) ;
32- } catch ( e ) { }
33- } else {
34- try {
22+ const callback = this . _owner ?. get ?.( ) ?. [ this . _propName ] ;
23+ if ( typeof callback === 'function' ) {
24+ if ( this . _propName === '_onToken' ) {
25+ callback ( message ) ;
26+ } else if ( this . _propName === '_onNotificationTap' || this . _propName === '_onMessage' ) {
27+ try {
28+ setTimeout ( ( ) => {
3529 callback ( JSON . parse ( message ) ) ;
36- } catch ( e ) { }
30+ } ) ;
31+ } catch ( e ) {
32+ // ignore
33+ }
34+ } else {
35+ try {
36+ callback ( JSON . parse ( message ) ) ;
37+ } catch ( e ) {
38+ // ignore
3739 }
3840 }
39- } ;
40- if ( ! MessagingCore . inForeground || ! MessagingCore . appDidLaunch ) {
41- MessagingCore . addToResumeQueue ( exec ) ;
42- } else {
43- exec ( ) ;
4441 }
4542 }
4643 }
@@ -58,23 +55,23 @@ let _permissionQueue: { resolve: Function; reject: Function }[] = [];
5855
5956function register ( args : any ) {
6057 if ( ! lastActivity ) {
61- // Some activities do not implement activity result API
62- if ( args . activity . registerForActivityResult ) {
63- lastActivity = new WeakRef ( args . activity ) ;
64- requestPermissionLauncher = args . activity . registerForActivityResult (
65- new androidx . activity . result . contract . ActivityResultContracts . RequestPermission ( ) ,
66- new androidx . activity . result . ActivityResultCallback ( {
67- onActivityResult ( isGranted : boolean ) {
68- _permissionQueue . forEach ( ( callback ) => {
69- callback . resolve ( isGranted ? 0 : 1 ) ;
70- } ) ;
71- _permissionQueue . splice ( 0 ) ;
72- } ,
73- } )
74- ) ;
75- } else {
76- Application . android . once ( 'activityCreated' , register ) ;
77- }
58+ // Some activities do not implement activity result API
59+ if ( args . activity . registerForActivityResult ) {
60+ lastActivity = new WeakRef ( args . activity ) ;
61+ requestPermissionLauncher = args . activity . registerForActivityResult (
62+ new androidx . activity . result . contract . ActivityResultContracts . RequestPermission ( ) ,
63+ new androidx . activity . result . ActivityResultCallback ( {
64+ onActivityResult ( isGranted : boolean ) {
65+ _permissionQueue . forEach ( ( callback ) => {
66+ callback . resolve ( isGranted ? 0 : 1 ) ;
67+ } ) ;
68+ _permissionQueue . splice ( 0 ) ;
69+ } ,
70+ } )
71+ ) ;
72+ } else {
73+ Application . android . once ( 'activityCreated' , register ) ;
74+ }
7875 }
7976}
8077
@@ -110,6 +107,8 @@ export class MessagingCore implements IMessagingCore {
110107 onMessageCallbacks . forEach ( ( cb ) => {
111108 cb ( message ) ;
112109 } ) ;
110+ } else {
111+ MessagingCore . _messageQueues . _onMessage . push ( message ) ;
113112 }
114113 }
115114
@@ -119,6 +118,8 @@ export class MessagingCore implements IMessagingCore {
119118 onNotificationTapCallbacks . forEach ( ( cb ) => {
120119 cb ( message ) ;
121120 } ) ;
121+ } else {
122+ MessagingCore . _messageQueues . _onNotificationTap . push ( message ) ;
122123 }
123124 }
124125
@@ -128,12 +129,19 @@ export class MessagingCore implements IMessagingCore {
128129 onTokenCallbacks . forEach ( ( cb ) => {
129130 cb ( token ) ;
130131 } ) ;
132+ } else {
133+ MessagingCore . _messageQueues . _onToken . push ( token ) ;
131134 }
132135 }
133136
134137 showNotificationsWhenInForeground : boolean ;
135138
136139 static _onResumeQueue = [ ] ;
140+ static _messageQueues = {
141+ _onMessage : [ ] ,
142+ _onNotificationTap : [ ] ,
143+ _onToken : [ ] ,
144+ } ;
137145 static addToResumeQueue ( callback : ( ) => void ) {
138146 if ( typeof callback !== 'function' ) {
139147 return ;
@@ -235,6 +243,7 @@ export class MessagingCore implements IMessagingCore {
235243 addOnMessage ( listener : ( message : any ) => any ) {
236244 if ( typeof listener === 'function' ) {
237245 onMessageCallbacks . add ( listener ) ;
246+ this . _triggerPendingCallbacks ( '_onMessage' ) ;
238247 }
239248 }
240249
@@ -248,6 +257,7 @@ export class MessagingCore implements IMessagingCore {
248257 addOnToken ( listener : ( token : string ) => any ) {
249258 if ( typeof listener === 'function' ) {
250259 onTokenCallbacks . add ( listener ) ;
260+ this . _triggerPendingCallbacks ( '_onToken' ) ;
251261 }
252262 }
253263
@@ -261,6 +271,7 @@ export class MessagingCore implements IMessagingCore {
261271 addOnNotificationTap ( listener : ( message : any ) => any ) {
262272 if ( typeof listener === 'function' ) {
263273 onNotificationTapCallbacks . add ( listener ) ;
274+ this . _triggerPendingCallbacks ( '_onNotificationTap' ) ;
264275 }
265276 }
266277
@@ -332,6 +343,16 @@ export class MessagingCore implements IMessagingCore {
332343 get isDeviceRegisteredForRemoteMessages ( ) : boolean {
333344 return org . nativescript . firebase . messaging . FirebaseMessaging . hasPermission ( Utils . android . getApplicationContext ( ) ) ;
334345 }
346+
347+ private _triggerPendingCallbacks ( type : keyof typeof MessagingCore . _messageQueues ) {
348+ const queue = MessagingCore . _messageQueues [ type ] ;
349+ if ( queue . length > 0 ) {
350+ MessagingCore . _messageQueues [ type ] = [ ] ;
351+ queue . forEach ( ( message ) => {
352+ this [ type ] ( message ) ;
353+ } ) ;
354+ }
355+ }
335356}
336357
337358export { AuthorizationStatus } from './common' ;
0 commit comments