Skip to content

Commit 39af577

Browse files
committed
fix(messaging): callback from auth
chore: improve simulator checks
1 parent b517b42 commit 39af577

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

packages/firebase-messaging/index.ios.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { Application, ApplicationSettings, Device } from '@nativescript/core';
22
import { deserialize, firebase, FirebaseApp, FirebaseError } from '@nativescript/firebase-core';
33
import { AuthorizationStatus, IMessaging, Permissions, Notification, RemoteMessage } from './common';
44

5-
65
export { AuthorizationStatus } from './common';
76

8-
declare const FIRApp, TNSFirebaseMessaging;
7+
declare const FIRApp, TNSFirebaseMessaging, FIRAuth;
98

109
let _registerDeviceForRemoteMessages = {
1110
resolve: null,
@@ -49,7 +48,7 @@ export class Messaging implements IMessaging {
4948
Application.ios.addNotificationObserver(UIApplicationDidFinishLaunchingNotification, (notification) => {
5049
UNUserNotificationCenterDelegateImpl.sharedInstance.observe();
5150
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;
5352
if (auto && !isSimulator) {
5453
UIApplication?.sharedApplication?.registerForRemoteNotifications?.();
5554
}
@@ -72,7 +71,7 @@ export class Messaging implements IMessaging {
7271

7372
getToken(): Promise<string> {
7473
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) {
7675
reject(new Error('You must be registered for remote messages before calling getToken, see messaging().registerDeviceForRemoteMessages()'));
7776
}
7877
this.native?.tokenWithCompletion((token, error) => {
@@ -85,7 +84,7 @@ export class Messaging implements IMessaging {
8584
});
8685
}
8786

88-
getAPNSToken(){
87+
getAPNSToken() {
8988
return TNSFirebaseMessaging.APNSTokenToString(this.native.APNSToken);
9089
}
9190

@@ -137,7 +136,7 @@ export class Messaging implements IMessaging {
137136

138137
registerDeviceForRemoteMessages(): Promise<void> {
139138
return new Promise((resolve, reject) => {
140-
if (UIDevice.currentDevice.name.toLocaleLowerCase().indexOf('simulator')) {
139+
if (UIDevice.currentDevice.name.toLocaleLowerCase().indexOf('simulator') > -1) {
141140
ApplicationSettings.setBoolean(REMOTE_NOTIFICATIONS_REGISTRATION_STATUS, true);
142141
resolve();
143142
}
@@ -358,14 +357,14 @@ class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNot
358357
userNotificationCenterWillPresentNotificationWithCompletionHandler(center: UNUserNotificationCenter, notification: UNNotification, completionHandler: (p1: UNNotificationPresentationOptions) => void): void {
359358
let options = UNNotificationPresentationOptionNone;
360359
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)) {
362361
options = UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge;
363362
}
364363

365364
if (notification.request.content.userInfo.objectForKey('gcm.message_id')) {
366365
const message = parseNotification(notification);
367366
if (!message['contentAvailable']) {
368-
defaultMessaging?._onMessage?.(message);
367+
(<any>firebase().messaging())?._onMessage?.(message);
369368
if (message) {
370369
message['foreground'] = UIApplication.sharedApplication.applicationState === UIApplicationState.Active;
371370
}
@@ -636,12 +635,19 @@ class AppDelegateImpl extends UIResponder implements UIApplicationDelegate {
636635
}
637636

638637
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+
639645
const message = parseRemoteMessage(userInfo);
640646
if (message) {
641647
message['foreground'] = application.applicationState === UIApplicationState.Active;
642648
}
643-
defaultMessaging?._onMessage?.(message);
644649
completionHandler(UIBackgroundFetchResult.NewData);
650+
(<any>firebase().messaging())._onMessage?.(message);
645651
}
646652

647653
applicationDidRegisterUserNotificationSettings(application: UIApplication, notificationSettings: UIUserNotificationSettings): void {

0 commit comments

Comments
 (0)