Skip to content

Commit

Permalink
feat(ios): trigger setFCMTokenWithCompletion & registerForRemoteNotif…
Browse files Browse the repository at this point in the history
…ications once during app launch
  • Loading branch information
erisu committed Sep 20, 2024
1 parent 6386944 commit 277466b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@interface PushPlugin ()

@property (nonatomic, strong) PushPluginFCM *pushPluginFCM;
@property (nonatomic, assign) BOOL isRegisteredForNotifications;

@end

Expand Down Expand Up @@ -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]];
}
Expand Down Expand Up @@ -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

0 comments on commit 277466b

Please sign in to comment.