Skip to content

Commit

Permalink
fix(messaging): replace simulator check
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Apr 1, 2022
1 parent 57d6c3e commit 70862a8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/firebase-core/platforms/ios/src/TNSFirebaseCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ typedef void(^LaunchCallback)();
@interface TNSFirebaseCore: NSObject
+(LaunchCallback) onAppFinishLaunchingCallback;
+(void) setOnAppFinishLaunchingCallback:(LaunchCallback)callback;
+(BOOL) isSimulator;
@end
8 changes: 8 additions & 0 deletions packages/firebase-core/platforms/ios/src/TNSFirebaseCore.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ + (LaunchCallback)onAppFinishLaunchingCallback {
+ (void)setOnAppFinishLaunchingCallback:(nullable LaunchCallback)callback {
_onAppFinishLaunchingCallback = callback;
}

+(BOOL) isSimulator {
#if TARGET_IPHONE_SIMULATOR
return true;
#else
return false;
#endif
}
@end
30 changes: 15 additions & 15 deletions packages/firebase-messaging/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AuthorizationStatus, IMessaging, Permissions, Notification, RemoteMessa

export { AuthorizationStatus } from './common';

declare const FIRApp, TNSFirebaseMessaging, FIRAuth, TNSUIApplicationDelegate;
declare const FIRApp, TNSFirebaseMessaging, TNSFirebaseCore;

let _registerDeviceForRemoteMessages = {
resolve: null,
Expand Down Expand Up @@ -60,8 +60,9 @@ export class Messaging implements IMessaging {

getToken(): Promise<string> {
return new Promise((resolve, reject) => {
if (UIDevice.currentDevice.name.toLocaleLowerCase().indexOf('simulator') !== -1 && !UIApplication.sharedApplication.registeredForRemoteNotifications) {
if (!TNSFirebaseCore.isSimulator() && !UIApplication.sharedApplication.registeredForRemoteNotifications) {
reject(new Error('You must be registered for remote messages before calling getToken, see messaging().registerDeviceForRemoteMessages()'));
return;
}
this.native?.tokenWithCompletion((token, error) => {
if (error) {
Expand Down Expand Up @@ -115,9 +116,8 @@ export class Messaging implements IMessaging {
this.#onMessage = listener;
if (listener) {
TNSFirebaseMessaging.onMessageCallback = (dict) => {
listener(deserialize(dict))
}

listener(deserialize(dict));
};
} else {
TNSFirebaseMessaging.onMessageCallback = null;
}
Expand All @@ -127,8 +127,8 @@ export class Messaging implements IMessaging {
this.#onToken = listener;
if (listener) {
TNSFirebaseMessaging.onTokenCallback = (value) => {
listener(value)
}
listener(value);
};
} else {
TNSFirebaseMessaging.onTokenCallback = null;
}
Expand All @@ -138,26 +138,26 @@ export class Messaging implements IMessaging {
this.#onNotificationTap = listener;
if (listener) {
TNSFirebaseMessaging.onNotificationTapCallback = (dict) => {
listener(deserialize(dict))
}
listener(deserialize(dict));
};
} else {
TNSFirebaseMessaging.onNotificationTapCallback = null;
}
}

registerDeviceForRemoteMessages(): Promise<void> {
return new Promise((resolve, reject) => {
if (UIDevice.currentDevice.name.toLocaleLowerCase().indexOf('simulator') > -1) {
if (TNSFirebaseCore.isSimulator()) {
ApplicationSettings.setBoolean(REMOTE_NOTIFICATIONS_REGISTRATION_STATUS, true);
resolve();
}
TNSFirebaseMessaging.registerDeviceForRemoteMessagesCallback = (result, error)=>{
if(error){
TNSFirebaseMessaging.registerDeviceForRemoteMessagesCallback = (result, error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}else {
} else {
resolve(result);
}
}
};
if (UIApplication?.sharedApplication) {
UIApplication?.sharedApplication?.registerForRemoteNotifications?.();
} else {
Expand Down Expand Up @@ -288,4 +288,4 @@ export class Messaging implements IMessaging {
get ios() {
return this.native;
}
}
}

0 comments on commit 70862a8

Please sign in to comment.