From 277466bc97ffcc17d6cefcc1f8c717f001926d17 Mon Sep 17 00:00:00 2001 From: Erisu Date: Fri, 20 Sep 2024 12:54:50 +0900 Subject: [PATCH] feat(ios): trigger setFCMTokenWithCompletion & registerForRemoteNotifications once during app launch --- src/ios/PushPlugin.m | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index ced1bac19..56df50daa 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -31,6 +31,7 @@ @interface PushPlugin () @property (nonatomic, strong) PushPluginFCM *pushPluginFCM; +@property (nonatomic, assign) BOOL isRegisteredForNotifications; @end @@ -186,7 +187,9 @@ - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { if ([self.pushPluginFCM isFCMEnabled]) { [self.pushPluginFCM setAPNSToken:deviceToken]; - [self setFCMTokenWithCompletion]; + if (![self isFirstLaunchAfterInstall]) { + [self setFCMTokenWithCompletion]; + } } else { [self registerWithToken:[self convertTokenToString:deviceToken]]; } @@ -486,7 +489,20 @@ - (void)handleNotificationSettingsWithAuthorizationOptions:(NSNumber *)authoriza } - (void)registerForRemoteNotifications { - [[UIApplication sharedApplication] registerForRemoteNotifications]; + if (!self.isRegisteredForNotifications) { + [[UIApplication sharedApplication] registerForRemoteNotifications]; + self.isRegisteredForNotifications = YES; + } +} + +- (BOOL)isFirstLaunchAfterInstall { + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + if (![defaults boolForKey:@"hasCordovaPushPluginLaunchedOnceAfterInstall"]) { + [defaults setBool:YES forKey:@"hasCordovaPushPluginLaunchedOnceAfterInstall"]; + [defaults synchronize]; + return YES; + } + return NO; } @end