From d7f771ed6dc0983b0d3e4db5b6d6992f8a967c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Bj=C3=B8rnskov?= <19725+henrikbjorn@users.noreply.github.com> Date: Thu, 23 Jul 2020 11:00:04 +0200 Subject: [PATCH] Pushkit completion (#645) The missing parts of the earlier pull request. --- lib/ios/RNPushKitEventHandler.m | 7 +++- lib/src/Notifications.ts | 2 +- lib/src/adapters/CompletionCallbackWrapper.ts | 4 +- lib/src/events/EventsRegistryIOS.test.ts | 38 ++++++++++++++++++- lib/src/events/EventsRegistryIOS.ts | 8 ++-- 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/lib/ios/RNPushKitEventHandler.m b/lib/ios/RNPushKitEventHandler.m index 3630a730e..94fa99ba1 100644 --- a/lib/ios/RNPushKitEventHandler.m +++ b/lib/ios/RNPushKitEventHandler.m @@ -16,9 +16,12 @@ - (void)registeredWithToken:(NSString *)token { } - (void)didReceiveIncomingPushWithPayload:(NSDictionary *)payload withCompletionHandler:(void (^)(void))completionHandler { - NSString *completionKey = [payload objectForKey:@"uuid"]; + NSString *identifier = [[NSUUID UUID] UUIDString]; - [_store setActionCompletionHandler:completionHandler withCompletionKey:completionKey]; + NSMutableDictionary *notification = [payload mutableCopy]; + notification[@"identifier"] = identifier; + + [_store setActionCompletionHandler:completionHandler withCompletionKey:identifier]; [RNEventEmitter sendEvent:RNPushKitNotificationReceived body:payload]; } diff --git a/lib/src/Notifications.ts b/lib/src/Notifications.ts index c859bd1cb..44956df90 100644 --- a/lib/src/Notifications.ts +++ b/lib/src/Notifications.ts @@ -37,7 +37,7 @@ export class NotificationsRoot { this.notificationFactory ); this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver, this.completionCallbackWrapper); - this.eventsRegistryIOS = new EventsRegistryIOS(this.nativeEventsReceiver); + this.eventsRegistryIOS = new EventsRegistryIOS(this.nativeEventsReceiver, this.completionCallbackWrapper); this._ios = new NotificationsIOS(this.commands, this.eventsRegistryIOS); this._android = new NotificationsAndroid(this.commands); diff --git a/lib/src/adapters/CompletionCallbackWrapper.ts b/lib/src/adapters/CompletionCallbackWrapper.ts index c860c1693..92e5aea03 100644 --- a/lib/src/adapters/CompletionCallbackWrapper.ts +++ b/lib/src/adapters/CompletionCallbackWrapper.ts @@ -37,11 +37,11 @@ export class CompletionCallbackWrapper { callback(notification, completion); } - public wrapOpenedCallback(callback: Function): (notification: Notification, actionResponse?: NotificationActionResponse) => void { + public wrapOpenedCallback(callback: Function): (notification: object, actionResponse?: NotificationActionResponse) => void { return (notification, actionResponse) => { const completion = () => { if (Platform.OS === 'ios') { - this.nativeCommandsSender.finishHandlingAction((notification as unknown as NotificationIOS).identifier); + this.nativeCommandsSender.finishHandlingAction((notification as NotificationIOS).identifier); } }; diff --git a/lib/src/events/EventsRegistryIOS.test.ts b/lib/src/events/EventsRegistryIOS.test.ts index 51be84171..44a6a068b 100644 --- a/lib/src/events/EventsRegistryIOS.test.ts +++ b/lib/src/events/EventsRegistryIOS.test.ts @@ -1,12 +1,16 @@ import { EventsRegistryIOS } from './EventsRegistryIOS'; +import { CompletionCallbackWrapper } from '../adapters/CompletionCallbackWrapper'; +import { NativeCommandsSender } from '../adapters/NativeCommandsSender.mock'; import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver.mock'; describe('EventsRegistryIOS', () => { let uut: EventsRegistryIOS; const mockNativeEventsReceiver = new NativeEventsReceiver(); + const mockNativeCommandsSender = new NativeCommandsSender(); + const completionCallbackWrapper = new CompletionCallbackWrapper(mockNativeCommandsSender); beforeEach(() => { - uut = new EventsRegistryIOS(mockNativeEventsReceiver); + uut = new EventsRegistryIOS(mockNativeEventsReceiver, completionCallbackWrapper); }); it('delegates registerPushKitRegistered to nativeEventsReceiver', () => { @@ -19,7 +23,37 @@ describe('EventsRegistryIOS', () => { it('delegates registerPushKitNotificationReceived to nativeEventsReceiver', () => { const cb = jest.fn(); uut.registerPushKitNotificationReceived(cb); + expect(mockNativeEventsReceiver.registerPushKitNotificationReceived).toHaveBeenCalledTimes(1); - expect(mockNativeEventsReceiver.registerPushKitNotificationReceived).toHaveBeenCalledWith(cb); + expect(mockNativeEventsReceiver.registerPushKitNotificationReceived).toHaveBeenCalledWith(expect.any(Function)); + + }); + + it('should wrap callback with completion block', () => { + const expectedNotification = { identifier: 'notificationId' } + + uut.registerPushKitNotificationReceived((notification) => { + expect(notification).toEqual(expectedNotification); + }); + + const call = mockNativeEventsReceiver.registerPushKitNotificationReceived.mock.calls[0][0]; + + call(expectedNotification); + }); + + it('should invoke finishPresentingNotification', () => { + const expectedNotification = { identifier: 'notificationId' }; + + uut.registerPushKitNotificationReceived((notification, completion) => { + completion(); + + expect(notification).toEqual(expectedNotification); + + expect(mockNativeCommandsSender.finishHandlingAction).toBeCalledWith('notificationId'); + }); + + const call = mockNativeEventsReceiver.registerPushKitNotificationReceived.mock.calls[0][0]; + + call(expectedNotification); }); }); diff --git a/lib/src/events/EventsRegistryIOS.ts b/lib/src/events/EventsRegistryIOS.ts index 4ea49fe53..c3f7c5247 100644 --- a/lib/src/events/EventsRegistryIOS.ts +++ b/lib/src/events/EventsRegistryIOS.ts @@ -3,17 +3,19 @@ import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver'; import { RegisteredPushKit } from '../interfaces/NotificationEvents'; +import { CompletionCallbackWrapper } from '../adapters/CompletionCallbackWrapper'; export class EventsRegistryIOS { constructor( - private nativeEventsReceiver: NativeEventsReceiver) + private nativeEventsReceiver: NativeEventsReceiver, + private completionCallbackWrapper: CompletionCallbackWrapper) {} public registerPushKitRegistered(callback: (event: RegisteredPushKit) => void): EmitterSubscription { return this.nativeEventsReceiver.registerPushKitRegistered(callback); } - public registerPushKitNotificationReceived(callback: (event: object) => void): EmitterSubscription { - return this.nativeEventsReceiver.registerPushKitNotificationReceived(callback); + public registerPushKitNotificationReceived(callback: (event: object, completion: () => void) => void): EmitterSubscription { + return this.nativeEventsReceiver.registerPushKitNotificationReceived(this.completionCallbackWrapper.wrapOpenedCallback(callback)); } }