Skip to content

Commit

Permalink
feat(ios): cleanup & trigger registerWithToken once each launch
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu committed Oct 15, 2024
1 parent 6386944 commit 6c03f00
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 34 deletions.
2 changes: 0 additions & 2 deletions src/ios/AppDelegate+notification.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#import "AppDelegate.h"
@import UserNotifications;

extern NSString *const pushPluginApplicationDidBecomeActiveNotification;

@interface AppDelegate (notification) <UNUserNotificationCenterDelegate>
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
Expand Down
4 changes: 0 additions & 4 deletions src/ios/AppDelegate+notification.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

static char launchNotificationKey;
static char coldstartKey;
NSString *const pushPluginApplicationDidBecomeActiveNotification = @"pushPluginApplicationDidBecomeActiveNotification";


@implementation AppDelegate (notification)

Expand Down Expand Up @@ -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
Expand Down
25 changes: 4 additions & 21 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
});
}
Expand Down Expand Up @@ -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];
Expand Down
2 changes: 0 additions & 2 deletions src/ios/PushPluginFCM.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ NS_ASSUME_NONNULL_BEGIN
- (void)unsubscribeFromTopic:(NSString *)topic;
- (void)unsubscribeFromTopics:(NSArray *)topics;

+ (NSNotificationName)pushPluginFCMMessagingRegistrationTokenRefreshedNotification;

@end

NS_ASSUME_NONNULL_END
8 changes: 3 additions & 5 deletions src/ios/PushPluginFCM.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -100,8 +102,4 @@ - (void)unsubscribeFromTopic:(NSString *)topic {
NSLog(@"[PushPlugin] Successfully unsubscribed from topic %@", topic);
}

+ (NSNotificationName)pushPluginFCMMessagingRegistrationTokenRefreshedNotification {
return FIRMessagingRegistrationTokenRefreshedNotification;
}

@end

0 comments on commit 6c03f00

Please sign in to comment.