From 6c03f003908d0f5f3322c28d724fed5a5ee2de3b Mon Sep 17 00:00:00 2001 From: Erisu Date: Fri, 20 Sep 2024 12:54:50 +0900 Subject: [PATCH] feat(ios): cleanup & trigger registerWithToken once each launch --- src/ios/AppDelegate+notification.h | 2 -- src/ios/AppDelegate+notification.m | 4 ---- src/ios/PushPlugin.m | 25 ++++--------------------- src/ios/PushPluginFCM.h | 2 -- src/ios/PushPluginFCM.m | 8 +++----- 5 files changed, 7 insertions(+), 34 deletions(-) diff --git a/src/ios/AppDelegate+notification.h b/src/ios/AppDelegate+notification.h index 9479b1839..4033a3f69 100644 --- a/src/ios/AppDelegate+notification.h +++ b/src/ios/AppDelegate+notification.h @@ -9,8 +9,6 @@ #import "AppDelegate.h" @import UserNotifications; -extern NSString *const pushPluginApplicationDidBecomeActiveNotification; - @interface AppDelegate (notification) - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; diff --git a/src/ios/AppDelegate+notification.m b/src/ios/AppDelegate+notification.m index 8229e5af6..cd5406b7e 100644 --- a/src/ios/AppDelegate+notification.m +++ b/src/ios/AppDelegate+notification.m @@ -12,8 +12,6 @@ static char launchNotificationKey; static char coldstartKey; -NSString *const pushPluginApplicationDidBecomeActiveNotification = @"pushPluginApplicationDidBecomeActiveNotification"; - @implementation AppDelegate (notification) @@ -180,8 +178,6 @@ - (void)pushPluginOnApplicationDidBecomeActive:(NSNotification *)notification { self.coldstart = [NSNumber numberWithBool:NO]; [pushHandler performSelectorOnMainThread:@selector(notificationReceived) withObject:pushHandler waitUntilDone:NO]; } - - [[NSNotificationCenter defaultCenter] postNotificationName:pushPluginApplicationDidBecomeActiveNotification object:nil]; } - (void)userNotificationCenter:(UNUserNotificationCenter *)center diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index ced1bac19..7e9c5f677 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -111,28 +111,20 @@ - (void)init:(CDVInvokedUrlCommand *)command { [[PushPluginSettings sharedInstance] updateSettingsWithOptions:[options objectForKey:@"ios"]]; PushPluginSettings *settings = [PushPluginSettings sharedInstance]; + self.callbackId = command.callbackId; + if ([settings voipEnabled]) { [self.commandDelegate runInBackground:^ { NSLog(@"[PushPlugin] VoIP set to true"); - - self.callbackId = command.callbackId; - PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()]; pushRegistry.delegate = self; pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; }]; } else { NSLog(@"[PushPlugin] VoIP missing or false"); - if ([self.pushPluginFCM isFCMEnabled]) { - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(setFCMTokenWithCompletion) - name:[PushPluginFCM pushPluginFCMMessagingRegistrationTokenRefreshedNotification] - object:nil]; - } [self.commandDelegate runInBackground:^ { NSLog(@"[PushPlugin] register called"); - self.callbackId = command.callbackId; self.isInline = NO; self.forceShow = [settings forceShowEnabled]; self.clearBadge = [settings clearBadgeEnabled]; @@ -161,14 +153,9 @@ - (void)init:(CDVInvokedUrlCommand *)command { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center setNotificationCategories:[settings categories]]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(handleNotificationSettings:) - name:pushPluginApplicationDidBecomeActiveNotification - object:nil]; - - if (notificationMessage) { // if there is a pending startup notification + // If there is a pending startup notification, we will delay to allow JS event handlers to setup + if (notificationMessage) { dispatch_async(dispatch_get_main_queue(), ^{ - // delay to allow JS event handlers to be setup [self performSelector:@selector(notificationReceived) withObject:nil afterDelay: 0.5]; }); } @@ -448,10 +435,6 @@ - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayloa [self notificationReceived]; } -- (void)handleNotificationSettings:(NSNotification *)notification { - [self handleNotificationSettingsWithAuthorizationOptions:nil]; -} - - (void)handleNotificationSettingsWithAuthorizationOptions:(NSNumber *)authorizationOptionsObject { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; UNAuthorizationOptions authorizationOptions = [authorizationOptionsObject unsignedIntegerValue]; diff --git a/src/ios/PushPluginFCM.h b/src/ios/PushPluginFCM.h index 7fc7695e8..6e000b37c 100644 --- a/src/ios/PushPluginFCM.h +++ b/src/ios/PushPluginFCM.h @@ -19,8 +19,6 @@ NS_ASSUME_NONNULL_BEGIN - (void)unsubscribeFromTopic:(NSString *)topic; - (void)unsubscribeFromTopics:(NSArray *)topics; -+ (NSNotificationName)pushPluginFCMMessagingRegistrationTokenRefreshedNotification; - @end NS_ASSUME_NONNULL_END diff --git a/src/ios/PushPluginFCM.m b/src/ios/PushPluginFCM.m index 779ab2c65..622e95301 100644 --- a/src/ios/PushPluginFCM.m +++ b/src/ios/PushPluginFCM.m @@ -30,7 +30,9 @@ - (instancetype)initWithGoogleServicePlist { - (void)configure { NSLog(@"[PushPlugin] Configuring Firebase App for FCM"); - [FIRApp configure]; + if (![FIRApp defaultApp]) { + [FIRApp configure]; + } } - (void)setAPNSToken:(NSData *)token { @@ -100,8 +102,4 @@ - (void)unsubscribeFromTopic:(NSString *)topic { NSLog(@"[PushPlugin] Successfully unsubscribed from topic %@", topic); } -+ (NSNotificationName)pushPluginFCMMessagingRegistrationTokenRefreshedNotification { - return FIRMessagingRegistrationTokenRefreshedNotification; -} - @end