Skip to content

Commit 80be0fd

Browse files
committed
iOS avoid taking control of UNUserNotificationCenter
Backport from react-native Origin-PR: mauron85/react-native-background-geolocation#268 Contributed-By: @danielgindi
1 parent 466b21f commit 80be0fd

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

ios/CDVBackgroundGeolocation/CDVBackgroundGeolocation.m

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ @implementation CDVBackgroundGeolocation {
2020
NSString *callbackId;
2121
MAURConfig *config;
2222
MAURBackgroundGeolocationFacade* facade;
23+
24+
API_AVAILABLE(ios(10.0))
25+
__weak id<UNUserNotificationCenterDelegate> prevNotificationDelegate;
2326
}
2427

2528
- (void)pluginInitialize
@@ -441,6 +444,18 @@ -(void) onFinishLaunching:(NSNotification *)notification
441444
}
442445

443446
NSDictionary *dict = [notification userInfo];
447+
448+
MAURConfig *config = [facade getConfig];
449+
if (config.isDebugging)
450+
{
451+
if (@available(iOS 10, *))
452+
{
453+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
454+
prevNotificationDelegate = center.delegate;
455+
center.delegate = self;
456+
}
457+
}
458+
444459
if ([dict objectForKey:UIApplicationLaunchOptionsLocationKey]) {
445460
NSLog(@"%@ %@", TAG, @"started by system on location event.");
446461
MAURConfig *config = [facade getConfig];
@@ -455,7 +470,18 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
455470
willPresentNotification:(UNNotification *)notification
456471
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
457472
{
458-
completionHandler(UNNotificationPresentationOptionAlert);
473+
if (prevNotificationDelegate && [prevNotificationDelegate respondsToSelector:@selector(userNotificationCenter:willPresentNotification:withCompletionHandler:)])
474+
{
475+
// Give other delegates (like FCM) the chance to process this notification
476+
477+
[prevNotificationDelegate userNotificationCenter:center willPresentNotification:notification withCompletionHandler:^(UNNotificationPresentationOptions options) {
478+
completionHandler(UNNotificationPresentationOptionAlert);
479+
}];
480+
}
481+
else
482+
{
483+
completionHandler(UNNotificationPresentationOptionAlert);
484+
}
459485
}
460486

461487
-(void) onAppTerminate:(NSNotification *)notification

0 commit comments

Comments
 (0)