@@ -2,10 +2,9 @@ import { Application, ApplicationSettings, Device } from '@nativescript/core';
2
2
import { deserialize , firebase , FirebaseApp , FirebaseError } from '@nativescript/firebase-core' ;
3
3
import { AuthorizationStatus , IMessaging , Permissions , Notification , RemoteMessage } from './common' ;
4
4
5
-
6
5
export { AuthorizationStatus } from './common' ;
7
6
8
- declare const FIRApp , TNSFirebaseMessaging ;
7
+ declare const FIRApp , TNSFirebaseMessaging , FIRAuth ;
9
8
10
9
let _registerDeviceForRemoteMessages = {
11
10
resolve : null ,
@@ -49,7 +48,7 @@ export class Messaging implements IMessaging {
49
48
Application . ios . addNotificationObserver ( UIApplicationDidFinishLaunchingNotification , ( notification ) => {
50
49
UNUserNotificationCenterDelegateImpl . sharedInstance . observe ( ) ;
51
50
const auto = ApplicationSettings . getBoolean ( REMOTE_NOTIFICATIONS_REGISTRATION_STATUS ) ?? false ;
52
- const isSimulator = UIDevice . currentDevice . name . toLocaleLowerCase ( ) . indexOf ( 'simulator' ) ;
51
+ const isSimulator = UIDevice . currentDevice . name . toLocaleLowerCase ( ) . indexOf ( 'simulator' ) > - 1 ;
53
52
if ( auto && ! isSimulator ) {
54
53
UIApplication ?. sharedApplication ?. registerForRemoteNotifications ?.( ) ;
55
54
}
@@ -72,7 +71,7 @@ export class Messaging implements IMessaging {
72
71
73
72
getToken ( ) : Promise < string > {
74
73
return new Promise ( ( resolve , reject ) => {
75
- if ( ! UIDevice . currentDevice . name . toLocaleLowerCase ( ) . indexOf ( 'simulator' ) && ! UIApplication . sharedApplication . registeredForRemoteNotifications ) {
74
+ if ( UIDevice . currentDevice . name . toLocaleLowerCase ( ) . indexOf ( 'simulator' ) !== - 1 && ! UIApplication . sharedApplication . registeredForRemoteNotifications ) {
76
75
reject ( new Error ( 'You must be registered for remote messages before calling getToken, see messaging().registerDeviceForRemoteMessages()' ) ) ;
77
76
}
78
77
this . native ?. tokenWithCompletion ( ( token , error ) => {
@@ -85,7 +84,7 @@ export class Messaging implements IMessaging {
85
84
} ) ;
86
85
}
87
86
88
- getAPNSToken ( ) {
87
+ getAPNSToken ( ) {
89
88
return TNSFirebaseMessaging . APNSTokenToString ( this . native . APNSToken ) ;
90
89
}
91
90
@@ -137,7 +136,7 @@ export class Messaging implements IMessaging {
137
136
138
137
registerDeviceForRemoteMessages ( ) : Promise < void > {
139
138
return new Promise ( ( resolve , reject ) => {
140
- if ( UIDevice . currentDevice . name . toLocaleLowerCase ( ) . indexOf ( 'simulator' ) ) {
139
+ if ( UIDevice . currentDevice . name . toLocaleLowerCase ( ) . indexOf ( 'simulator' ) > - 1 ) {
141
140
ApplicationSettings . setBoolean ( REMOTE_NOTIFICATIONS_REGISTRATION_STATUS , true ) ;
142
141
resolve ( ) ;
143
142
}
@@ -358,14 +357,14 @@ class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNot
358
357
userNotificationCenterWillPresentNotificationWithCompletionHandler ( center : UNUserNotificationCenter , notification : UNNotification , completionHandler : ( p1 : UNNotificationPresentationOptions ) => void ) : void {
359
358
let options = UNNotificationPresentationOptionNone ;
360
359
const aps = notification . request . content . userInfo . objectForKey ( 'aps' ) as NSDictionary < any , any > ;
361
- if ( defaultMessaging ? .showNotificationsWhenInForeground || notification . request . content . userInfo . objectForKey ( 'gcm.notification.showWhenInForeground' ) === 'true' || notification . request . content . userInfo . objectForKey ( 'showWhenInForeground' ) === true || ( aps && aps . objectForKey ( 'showWhenInForeground' ) === true ) ) {
360
+ if ( firebase ( ) . messaging ( ) . showNotificationsWhenInForeground || notification . request . content . userInfo . objectForKey ( 'gcm.notification.showWhenInForeground' ) === 'true' || notification . request . content . userInfo . objectForKey ( 'showWhenInForeground' ) === true || ( aps && aps . objectForKey ( 'showWhenInForeground' ) === true ) ) {
362
361
options = UNNotificationPresentationOptions . Alert | UNNotificationPresentationOptions . Sound | UNNotificationPresentationOptions . Badge ;
363
362
}
364
363
365
364
if ( notification . request . content . userInfo . objectForKey ( 'gcm.message_id' ) ) {
366
365
const message = parseNotification ( notification ) ;
367
366
if ( ! message [ 'contentAvailable' ] ) {
368
- defaultMessaging ?. _onMessage ?.( message ) ;
367
+ ( < any > firebase ( ) . messaging ( ) ) ?. _onMessage ?.( message ) ;
369
368
if ( message ) {
370
369
message [ 'foreground' ] = UIApplication . sharedApplication . applicationState === UIApplicationState . Active ;
371
370
}
@@ -636,12 +635,19 @@ class AppDelegateImpl extends UIResponder implements UIApplicationDelegate {
636
635
}
637
636
638
637
applicationDidReceiveRemoteNotificationFetchCompletionHandler ( application : UIApplication , userInfo : NSDictionary < any , any > , completionHandler : ( p1 : UIBackgroundFetchResult ) => void ) : void {
638
+ if ( typeof FIRAuth !== undefined ) {
639
+ if ( FIRAuth . auth ( ) . canHandleNotification ( userInfo ) ) {
640
+ completionHandler ( UIBackgroundFetchResult . NoData ) ;
641
+ return ;
642
+ }
643
+ }
644
+
639
645
const message = parseRemoteMessage ( userInfo ) ;
640
646
if ( message ) {
641
647
message [ 'foreground' ] = application . applicationState === UIApplicationState . Active ;
642
648
}
643
- defaultMessaging ?. _onMessage ?.( message ) ;
644
649
completionHandler ( UIBackgroundFetchResult . NewData ) ;
650
+ ( < any > firebase ( ) . messaging ( ) ) . _onMessage ?.( message ) ;
645
651
}
646
652
647
653
applicationDidRegisterUserNotificationSettings ( application : UIApplication , notificationSettings : UIUserNotificationSettings ) : void {
0 commit comments