Skip to content

Commit

Permalink
Pushkit completion (#645)
Browse files Browse the repository at this point in the history
The missing parts of the earlier pull request.
  • Loading branch information
henrikbjorn authored Jul 23, 2020
1 parent 54d6bc2 commit d7f771e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
7 changes: 5 additions & 2 deletions lib/ios/RNPushKitEventHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/Notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/adapters/CompletionCallbackWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down
38 changes: 36 additions & 2 deletions lib/src/events/EventsRegistryIOS.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
});
8 changes: 5 additions & 3 deletions lib/src/events/EventsRegistryIOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit d7f771e

Please sign in to comment.