[ios][notifications] fix completion crash: #1576

This commit is contained in:
Salakar
2018-10-16 18:14:32 +01:00
parent 4b0486e58a
commit 4b8d840648
5 changed files with 86 additions and 18 deletions

View File

@@ -1,9 +1,16 @@
#import "RCTConvert+UIBackgroundFetchResult.h"
@implementation RCTConvert (UIBackgroundFetchResult)
RCT_ENUM_CONVERTER(UIBackgroundFetchResult, (@{ @"backgroundFetchResultNoData" : @(UIBackgroundFetchResultNoData),
@"backgroundFetchResultNewData" : @(UIBackgroundFetchResultNewData),
@"backgroundFetchResultFailed" : @(UIBackgroundFetchResultFailed)}),
UIBackgroundFetchResultNoData, integerValue)
RCT_ENUM_CONVERTER(
UIBackgroundFetchResult,
(@{
@"backgroundFetchResultNoData" : @(UIBackgroundFetchResultNoData),
@"backgroundFetchResultNewData" : @(UIBackgroundFetchResultNewData),
@"backgroundFetchResultFailed" : @(UIBackgroundFetchResultFailed)}
),
UIBackgroundFetchResultNoData,
integerValue
)
@end

View File

@@ -99,11 +99,12 @@ RCT_EXPORT_MODULE();
}
RCT_EXPORT_METHOD(complete:(NSString*)handlerKey fetchResult:(UIBackgroundFetchResult)fetchResult) {
void (^completionHandler)(UIBackgroundFetchResult) = completionHandlers[handlerKey];
completionHandlers[handlerKey] = nil;
if(completionHandler != nil) {
completionHandler(fetchResult);
if (handlerKey != nil) {
void (^completionHandler)(UIBackgroundFetchResult) = completionHandlers[handlerKey];
if(completionHandler != nil) {
completionHandlers[handlerKey] = nil;
completionHandler(fetchResult);
}
}
}
@@ -142,12 +143,16 @@ RCT_EXPORT_METHOD(complete:(NSString*)handlerKey fetchResult:(UIBackgroundFetchR
// For onOpened events, we set the default action name as iOS 8/9 has no concept of actions
if (event == NOTIFICATIONS_NOTIFICATION_OPENED) {
notification = @{
@"action": DEFAULT_ACTION,
@"notification": notification
};
@"action": DEFAULT_ACTION,
@"notification": notification
};
}
if (handlerKey != nil) {
completionHandlers[handlerKey] = completionHandler;
} else {
completionHandler(UIBackgroundFetchResultNoData);
}
completionHandlers[handlerKey] = completionHandler;
[self sendJSEvent:self name:event body:notification];
}

View File

@@ -61,10 +61,12 @@ export default class IOSNotification {
if (isIOS && notifications && notifications.ios) {
const complete = (fetchResult: BackgroundFetchResultValue) => {
const { notificationId } = notification;
getLogger(notifications).debug(
`Completion handler called for notificationId=${notificationId}`
);
getNativeModule(notifications).complete(notificationId, fetchResult);
if (notificationId) {
getLogger(notifications).debug(
`Completion handler called for notificationId=${notificationId}`
);
getNativeModule(notifications).complete(notificationId, fetchResult);
}
};
if (notifications.ios.shouldAutoComplete) {

View File

@@ -55,6 +55,7 @@ export default class Notification {
this,
nativeNotification && nativeNotification.android
);
this._ios = new IOSNotification(
this,
notifications,

View File

@@ -0,0 +1,53 @@
const trigger = {
type: 'push',
};
const notification = {
trigger,
title: 'Foo',
subtitle: 'Bar',
body: 'World',
// badge: 0,
payload: {
foo: 'world',
},
// category: '',
// 'content-available': 0,
// 'user-text': '',
// 'action-identifier': '',
};
describe('notifications() - iOS Only', () => {
describe('getInitialNotification()', () => {
it('should be provided ', async () => {
if (device.getPlatform() === 'ios') {
await device.relaunchApp({ userNotification: notification });
const initialNotification = await firebase
.notifications()
.getInitialNotification();
initialNotification.action.should.equal(
'com.apple.UNNotificationDefaultActionIdentifier'
);
initialNotification.notification.should.be.an.instanceOf(
jet.require('src/modules/notifications/Notification')
);
initialNotification.notification.title.should.equal(notification.title);
initialNotification.notification.subtitle.should.equal(
notification.subtitle
);
initialNotification.notification.body.should.equal(notification.body);
initialNotification.notification.data.foo.should.equal(
notification.payload.foo
);
// TODO: Salakar: more validations
}
});
});
});