Skip to content

Commit d8bc984

Browse files
committed
fix(ios): deliver payload to frontend handler when app in background
1 parent d50a41b commit d8bc984

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

docs/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ The event `notification` will be triggered each time a push notification is rece
389389
| `data.additionalData.foreground` | `boolean` | Whether the notification was received while the app was in the foreground |
390390
| `data.additionalData.coldstart` | `boolean` | Will be `true` if the application is started by clicking on the push notification, `false` if the app is already started. |
391391
| `data.additionalData.dismissed` | `boolean` | Is set to `true` if the notification was dismissed by the user |
392+
| `data.additionalData.applicationState` | `number` | (iOS Only Flag) Contains the application state of the received notification. 0: `UIApplicationStateActive`, 1: `UIApplicationStateInactive`, 2: `UIApplicationStateBackground` |
392393

393394
### Example
394395

src/ios/PushPlugin.m

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ - (void)init:(CDVInvokedUrlCommand *)command {
192192
[center setNotificationCategories:[settings categories]];
193193

194194
// If there is a pending startup notification, we will delay to allow JS event handlers to setup
195-
if (self.notificationMessage && !self.isInitialized) {
196-
dispatch_async(dispatch_get_main_queue(), ^{
197-
[self performSelector:@selector(notificationReceived) withObject:nil afterDelay: 0.5];
195+
if (self.notificationMessage) {
196+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
197+
[self notificationReceived:@YES];
198198
});
199199
}
200200

@@ -489,6 +489,10 @@ - (void)didReceiveNotificationResponse:(NSNotification *)notification {
489489
}
490490

491491
- (void)notificationReceived {
492+
[self notificationReceived:@NO];
493+
}
494+
495+
- (void)notificationReceived:(NSNumber *)forceClear {
492496
NSLog(@"[PushPlugin] Notification received");
493497

494498
if (self.notificationMessage && self.callbackId != nil)
@@ -502,9 +506,7 @@ - (void)notificationReceived {
502506
if ([[mutableNotificationMessage objectForKey:@"actionCallback"] isEqualToString:UNNotificationDefaultActionIdentifier]) {
503507
[mutableNotificationMessage removeObjectForKey:@"actionCallback"];
504508
}
505-
// @todo do not sent applicationState data to front for now. Figure out if we can add
506-
// similar data to the other platforms.
507-
[mutableNotificationMessage removeObjectForKey:@"applicationState"];
509+
508510
self.notificationMessage = [mutableNotificationMessage copy];
509511

510512
for (id key in self.notificationMessage) {
@@ -567,9 +569,26 @@ - (void)notificationReceived {
567569
[pluginResult setKeepCallbackAsBool:YES];
568570
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
569571

572+
// When application state is inactive, do not clear notificationMessage.
573+
// The init method will trigger when returning to foreground and trigger notification process.
574+
NSNumber *applicationState = self.notificationMessage[@"applicationState"];
575+
if (applicationState && [applicationState isKindOfClass:[NSNumber class]]) {
576+
if ([applicationState integerValue] != UIApplicationStateInactive) {
577+
self.notificationMessage = nil;
578+
}
579+
} else {
580+
// Handle unexpected cases where applicationState is missing or invalid
581+
self.notificationMessage = nil;
582+
}
583+
584+
// Force clear comes from init. This ensure that it will be processed once.
585+
BOOL shouldForceClear = [forceClear boolValue];
586+
if (shouldForceClear) {
587+
self.notificationMessage = nil;
588+
}
589+
570590
self.coldstart = NO;
571591
self.isForeground = NO;
572-
self.notificationMessage = nil;
573592
}
574593
}
575594

0 commit comments

Comments
 (0)