mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-21 02:26:30 +08:00
[ios][notifications] fix completion crash: #1576
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -55,6 +55,7 @@ export default class Notification {
|
||||
this,
|
||||
nativeNotification && nativeNotification.android
|
||||
);
|
||||
|
||||
this._ios = new IOSNotification(
|
||||
this,
|
||||
notifications,
|
||||
|
||||
53
tests/e2e/notifications/ios.e2e.js
Normal file
53
tests/e2e/notifications/ios.e2e.js
Normal 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
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user