Add exported native function to complete handling of ios notification
This commit is contained in:
parent
7a66c165ed
commit
e6cd793f91
@ -16,7 +16,9 @@
|
|||||||
#endif
|
#endif
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation RNFirebaseNotifications
|
@implementation RNFirebaseNotifications {
|
||||||
|
NSMutableDictionary<NSString *, void (^)(UIBackgroundFetchResult)> *completionHandlers;
|
||||||
|
}
|
||||||
|
|
||||||
static RNFirebaseNotifications *theRNFirebaseNotifications = nil;
|
static RNFirebaseNotifications *theRNFirebaseNotifications = nil;
|
||||||
// PRE-BRIDGE-EVENTS: Consider enabling this to allow events built up before the bridge is built to be sent to the JS side
|
// PRE-BRIDGE-EVENTS: Consider enabling this to allow events built up before the bridge is built to be sent to the JS side
|
||||||
@ -54,6 +56,8 @@ RCT_EXPORT_MODULE();
|
|||||||
|
|
||||||
// Set static instance for use from AppDelegate
|
// Set static instance for use from AppDelegate
|
||||||
theRNFirebaseNotifications = self;
|
theRNFirebaseNotifications = self;
|
||||||
|
|
||||||
|
completionHandlers = [[NSMutableDictionary alloc] init];
|
||||||
}
|
}
|
||||||
|
|
||||||
// PRE-BRIDGE-EVENTS: Consider enabling this to allow events built up before the bridge is built to be sent to the JS side
|
// PRE-BRIDGE-EVENTS: Consider enabling this to allow events built up before the bridge is built to be sent to the JS side
|
||||||
@ -95,6 +99,24 @@ RCT_EXPORT_MODULE();
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RCT_EXPORT_METHOD(complete:(NSString*)handlerKey fetchResult:(NSString *)rnFetchResult) {
|
||||||
|
UIBackgroundFetchResult fetchResult = UIBackgroundFetchResultNoData;
|
||||||
|
if ([@"noData" isEqualToString:rnFetchResult]) {
|
||||||
|
fetchResult = UIBackgroundFetchResultNoData;
|
||||||
|
} else if ([@"newData" isEqualToString:rnFetchResult]) {
|
||||||
|
fetchResult = UIBackgroundFetchResultNewData;
|
||||||
|
} else if ([@"failed" isEqualToString:rnFetchResult]) {
|
||||||
|
fetchResult = UIBackgroundFetchResultFailed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void (^completionHandler)(UIBackgroundFetchResult) = completionHandlers[handlerKey];
|
||||||
|
completionHandlers[handlerKey] = nil;
|
||||||
|
|
||||||
|
if(completionHandler != nil) {
|
||||||
|
completionHandler(fetchResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Listen for background messages
|
// Listen for background messages
|
||||||
- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo
|
- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo
|
||||||
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
|
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
|
||||||
@ -102,7 +124,6 @@ RCT_EXPORT_MODULE();
|
|||||||
// Pass them over to the RNFirebaseMessaging handler instead
|
// Pass them over to the RNFirebaseMessaging handler instead
|
||||||
if (userInfo[@"aps"] && ((NSDictionary*)userInfo[@"aps"]).count == 1 && userInfo[@"aps"][@"content-available"]) {
|
if (userInfo[@"aps"] && ((NSDictionary*)userInfo[@"aps"]).count == 1 && userInfo[@"aps"][@"content-available"]) {
|
||||||
[[RNFirebaseMessaging instance] didReceiveRemoteNotification:userInfo];
|
[[RNFirebaseMessaging instance] didReceiveRemoteNotification:userInfo];
|
||||||
completionHandler(UIBackgroundFetchResultNoData);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +141,6 @@ RCT_EXPORT_MODULE();
|
|||||||
// - foreground notifications also go through willPresentNotification
|
// - foreground notifications also go through willPresentNotification
|
||||||
// - background notification presses also go through didReceiveNotificationResponse
|
// - background notification presses also go through didReceiveNotificationResponse
|
||||||
// This prevents duplicate messages from hitting the JS app
|
// This prevents duplicate messages from hitting the JS app
|
||||||
completionHandler(UIBackgroundFetchResultNoData);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,8 +153,10 @@ RCT_EXPORT_MODULE();
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NSString *handlerKey = notification[@"notificationId"];
|
||||||
|
completionHandlers[handlerKey] = completionHandler;
|
||||||
|
|
||||||
[self sendJSEvent:self name:event body:notification];
|
[self sendJSEvent:self name:event body:notification];
|
||||||
completionHandler(UIBackgroundFetchResultNoData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// *******************************************************
|
// *******************************************************
|
||||||
@ -226,7 +248,7 @@ RCT_EXPORT_METHOD(cancelNotification:(NSString*) notificationId
|
|||||||
if ([self isIOS89]) {
|
if ([self isIOS89]) {
|
||||||
for (UILocalNotification *notification in RCTSharedApplication().scheduledLocalNotifications) {
|
for (UILocalNotification *notification in RCTSharedApplication().scheduledLocalNotifications) {
|
||||||
NSDictionary *notificationInfo = notification.userInfo;
|
NSDictionary *notificationInfo = notification.userInfo;
|
||||||
if ([notificationId isEqualToString:[notificationInfo valueForKey:@"notificationId"]]) {
|
if ([notificationId isEqualToString:notificationInfo[@"notificationId"]]) {
|
||||||
[RCTSharedApplication() cancelLocalNotification:notification];
|
[RCTSharedApplication() cancelLocalNotification:notification];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -200,6 +200,10 @@ export default class Notifications extends ModuleBase {
|
|||||||
return getNativeModule(this).getScheduledNotifications();
|
return getNativeModule(this).getScheduledNotifications();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
complete(handlerId: string, fetchResult: string): Promise<void> {
|
||||||
|
return getNativeModule(this).complete(handlerId, fetchResult);
|
||||||
|
}
|
||||||
|
|
||||||
onNotification(
|
onNotification(
|
||||||
nextOrObserver: OnNotification | OnNotificationObserver
|
nextOrObserver: OnNotification | OnNotificationObserver
|
||||||
): () => any {
|
): () => any {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user