Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): update notification event logic #301

Draft
wants to merge 1 commit into
base: feat/use-cdvappdelegate
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,18 @@ - (void)pushPluginOnApplicationDidBecomeActive:(NSNotification *)notification {
- (void)willPresentNotification:(NSNotification *)notification {
NSLog(@"[PushPlugin] Notification was received while the app was in the foreground. (willPresentNotification)");

UIApplicationState applicationState = [UIApplication sharedApplication].applicationState;
NSNumber *applicationStateNumber = @((int)applicationState);

// The original notification that comes from the CDVAppDelegate's willPresentNotification.
UNNotification *originalNotification = notification.userInfo[@"notification"];
NSDictionary *userInfo = originalNotification.request.content.userInfo;
NSDictionary *originalUserInfo = originalNotification.request.content.userInfo;
NSMutableDictionary *modifiedUserInfo = [originalUserInfo mutableCopy];
[modifiedUserInfo setObject:applicationStateNumber forKey:@"applicationState"];

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

self.notificationMessage = userInfo;
self.notificationMessage = modifiedUserInfo;
self.isInline = YES;
[self notificationReceived];

Expand All @@ -367,11 +372,13 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {

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

UIApplicationState applicationState = [UIApplication sharedApplication].applicationState;
NSNumber *applicationStateNumber = @((int)applicationState);
NSDictionary *originalUserInfo = response.notification.request.content.userInfo;
NSMutableDictionary *modifiedUserInfo = [originalUserInfo mutableCopy];
[modifiedUserInfo setObject:response.actionIdentifier forKey:@"actionCallback"];
[modifiedUserInfo setObject:applicationStateNumber forKey:@"applicationState"];

switch ([UIApplication sharedApplication].applicationState) {
switch (applicationState) {
case UIApplicationStateActive:
{
self.isInline = NO;
Expand All @@ -388,12 +395,7 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
case UIApplicationStateInactive:
{
self.coldstart = YES;

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

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

Expand Down Expand Up @@ -427,7 +429,7 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
[self.handlerObj setObject:safeHandler forKey:@"handler"];
}

self.notificationMessage = originalUserInfo;
self.notificationMessage = modifiedUserInfo;

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

Expand All @@ -445,6 +447,16 @@ - (void)notificationReceived {
NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:4];
NSMutableDictionary* additionalData = [NSMutableDictionary dictionaryWithCapacity:4];

// Remove "actionCallback" when application state is not foreground. Only applied to foreground.
NSNumber *applicationStateNumber = self.notificationMessage[@"applicationState"];
UIApplicationState applicationState = (UIApplicationState)[applicationStateNumber intValue];
if (applicationState != UIApplicationStateActive) {
[(NSMutableDictionary *) self.notificationMessage removeObjectForKey:@"actionCallback"];
}
// @todo do not sent applicationState data to front for now. Figure out if we can add
// similar data to the other platforms.
[(NSMutableDictionary *) self.notificationMessage removeObjectForKey:@"applicationState"];

for (id key in self.notificationMessage) {
if ([key isEqualToString:@"aps"]) {
id aps = [self.notificationMessage objectForKey:@"aps"];
Expand Down
Loading