Skip to content

Commit

Permalink
feat(ios)!: pass everything and format in plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu committed Nov 12, 2024
1 parent f251f49 commit 98a98e0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
21 changes: 6 additions & 15 deletions src/ios/CDVAppDelegate+notification.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ @implementation CDVAppDelegate (notification)

// its dangerous to override a method from within a category.
// Instead we will use method swizzling. we set this up in the load call.
+ (void)load
{
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
Expand Down Expand Up @@ -41,8 +40,7 @@ + (void)load
});
}

- (CDVAppDelegate *)pushPluginSwizzledInit
{
- (CDVAppDelegate *)pushPluginSwizzledInit {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;

Expand All @@ -60,8 +58,6 @@ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotif
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

// Create a dictionary with userInfo and the completionHandler
NSDictionary *notificationInfo = @{
@"userInfo": userInfo,
@"completionHandler": completionHandler
Expand All @@ -71,25 +67,20 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSMutableDictionary *userInfo = [notification.request.content.userInfo mutableCopy];
NSDictionary *notificationInfo = @{
@"userInfo": userInfo,
@"notification": notification,
@"completionHandler": completionHandler
};

[NSNotificationCenter.defaultCenter postNotificationName:@"CordovaPluginPushWillPresentNotification" object:nil userInfo:notificationInfo];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
NSLog(@"Push Plugin didReceiveNotificationResponse: actionIdentifier %@, notification: %@", response.actionIdentifier, response.notification.request.content.userInfo);
NSDictionary *originalUserInfo = response.notification.request.content.userInfo;
NSMutableDictionary *modifiedUserInfo = [originalUserInfo mutableCopy];
[modifiedUserInfo setObject:response.actionIdentifier forKey:@"actionCallback"];
NSDictionary *notificationInfo = @{
@"actionIdentifier": response.actionIdentifier,
@"originalUserInfo": originalUserInfo,
@"modifiedUserInfo": modifiedUserInfo,
@"response": response,
@"completionHandler": completionHandler
};

[NSNotificationCenter.defaultCenter postNotificationName:@"CordovaPluginPushDidReceiveNotificationResponse" object:nil userInfo:notificationInfo];
}

Expand Down
72 changes: 50 additions & 22 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSNotification *)notif
NSData *deviceToken = notification.object;

if (self.callbackId == nil) {
NSLog(@"[PushPlugin] Unexpected call to didRegisterForRemoteNotificationsWithDeviceToken, ignoring: %@", deviceToken);
NSLog(@"[PushPlugin] An unexpected case was triggered where the callbackId is missing during the register for remote notification. (device token: %@)", deviceToken);
return;
}

NSLog(@"[PushPlugin] register success: %@", deviceToken);
NSLog(@"[PushPlugin] Successfully registered device for remote notification. (device token: %@)", deviceToken);

if ([self.pushPluginFCM isFCMEnabled]) {
[self.pushPluginFCM configureTokens:deviceToken];
Expand Down Expand Up @@ -242,19 +242,21 @@ - (void)didFailToRegisterForRemoteNotificationsWithError:(NSNotification *)notif
NSError *error = (NSError *)notification.object;

if (self.callbackId == nil) {
NSLog(@"[PushPlugin] Unexpected call to didFailToRegisterForRemoteNotificationsWithError, ignoring: %@", error);
NSLog(@"[PushPlugin] An unexpected case was triggered where the callbackId is missing during the failure to register for remote notification. (error: %@)", error);
return;
}
NSLog(@"[PushPlugin] register failed");
[self failWithMessage:self.callbackId withMsg:@"" withError:error];

NSLog(@"[PushPlugin] Failed to register for remote notification with error: %@", error);
[self failWithMessage:self.callbackId withMsg:@"Failed to register for remote notification." withError:error];
}

- (void)didReceiveRemoteNotification:(NSNotification *)notification {
NSDictionary *notificationInfo = notification.userInfo;
// Extract userInfo and completionHandler
NSDictionary *userInfo = notificationInfo[@"userInfo"];
void (^completionHandler)(UIBackgroundFetchResult) = notificationInfo[@"completionHandler"];
NSLog(@"[PushPlugin]didReceiveNotification with fetchCompletionHandler");
NSDictionary *userInfo = notification.userInfo[@"userInfo"];

NSLog(@"[PushPlugin] Received remote notification (userInfo: %@)", userInfo);

void (^completionHandler)(UIBackgroundFetchResult) = notification.userInfo[@"completionHandler"];

// app is in the background or inactive, so only call notification callback if this is a silent push
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
NSLog(@"[PushPlugin] app in-active");
Expand Down Expand Up @@ -289,7 +291,8 @@ - (void)didReceiveRemoteNotification:(NSNotification *)notification {
self.isInline = NO;
[self notificationReceived];
} else {
NSLog(@"[PushPlugin] Save push for later");
NSLog(@"[PushPlugin] Application is not active, saving notification for later.");

self.launchNotification = userInfo;
completionHandler(UIBackgroundFetchResultNewData);
}
Expand Down Expand Up @@ -328,10 +331,13 @@ - (void)pushPluginOnApplicationDidBecomeActive:(NSNotification *)notification {
}

- (void)willPresentNotification:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo[@"userInfo"];
void (^completionHandler)(UNNotificationPresentationOptions) = notification.userInfo[@"completionHandler"];
NSLog(@"[PushPlugin] Notification was received while the app was in the foreground. (willPresentNotification)");

NSLog(@"[PushPlugin] NotificationCenter Handle push from foreground");
// The original notification that comes from the CDVAppDelegate's willPresentNotification.
UNNotification *originalNotification = notification.userInfo[@"notification"];
NSDictionary *userInfo = originalNotification.request.content.userInfo;

void (^completionHandler)(UNNotificationPresentationOptions) = notification.userInfo[@"completionHandler"];

self.notificationMessage = userInfo;
self.isInline = YES;
Expand All @@ -350,15 +356,27 @@ - (void)willPresentNotification:(NSNotification *)notification {
}

- (void)didReceiveNotificationResponse:(NSNotification *)notification {
NSDictionary *originalUserInfo = notification.userInfo[@"originalUserInfo"];
NSDictionary *modifiedUserInfo = notification.userInfo[@"modifiedUserInfo"];
// The original response that comes from the CDVAppDelegate's didReceiveNotificationResponse.
UNNotificationResponse *response = notification.userInfo[@"response"];

NSLog(@"[PushPlugin] Notification was received. (actionIdentifier %@, notification: %@)",
response.actionIdentifier,
response.notification.request.content.userInfo);

void (^completionHandler)(void) = notification.userInfo[@"completionHandler"];
NSLog(@"[PushPlugin] Modified UserInfo %@", modifiedUserInfo);

NSDictionary *originalUserInfo = response.notification.request.content.userInfo;
NSMutableDictionary *modifiedUserInfo = [originalUserInfo mutableCopy];
[modifiedUserInfo setObject:response.actionIdentifier forKey:@"actionCallback"];

switch ([UIApplication sharedApplication].applicationState) {
case UIApplicationStateActive:
{
self.notificationMessage = modifiedUserInfo;
self.isInline = NO;
self.notificationMessage = modifiedUserInfo;

NSLog(@"[PushPlugin] App is active. Notification message set with: %@", self.notificationMessage);

[self notificationReceived];
if (completionHandler) {
completionHandler();
Expand All @@ -367,13 +385,16 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
}
case UIApplicationStateInactive:
{
NSLog(@"[PushPlugin] coldstart");
self.coldstart = YES;

if ([notification.userInfo[@"actionIdentifier"] rangeOfString:@"UNNotificationDefaultActionIdentifier"].location == NSNotFound) {
self.launchNotification = modifiedUserInfo;
} else {
self.launchNotification = originalUserInfo;
}
self.coldstart = YES;

NSLog(@"[PushPlugin] App is inactive. Storing notification message for later launch with: %@", self.launchNotification);

if (completionHandler) {
completionHandler();
}
Expand All @@ -388,9 +409,13 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
}
});
};

self.isInline = NO;

if (self.handlerObj == nil) {
self.handlerObj = [NSMutableDictionary dictionaryWithCapacity:2];
}

id notId = modifiedUserInfo[@"notId"];
if (notId != nil) {
NSLog(@"[PushPlugin] notId %@", notId);
Expand All @@ -399,8 +424,11 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
NSLog(@"[PushPlugin] notId handler");
[self.handlerObj setObject:safeHandler forKey:@"handler"];
}
self.notificationMessage = modifiedUserInfo;
self.isInline = NO;

self.notificationMessage = originalUserInfo;

NSLog(@"[PushPlugin] App is in the background. Notification message set with: %@", self.notificationMessage);

[self performSelectorOnMainThread:@selector(notificationReceived) withObject:self waitUntilDone:NO];
break;
}
Expand Down

0 comments on commit 98a98e0

Please sign in to comment.