Move autocompletion into IOSNotification class

This commit is contained in:
Ryan Grey 2018-08-14 16:34:31 +01:00
parent 000a4ec724
commit 0af7c02955
3 changed files with 56 additions and 37 deletions

View File

@ -3,11 +3,18 @@
* IOSNotification representation wrapper
*/
import type Notification from './Notification';
import IOSNotifications, {
type BackgroundFetchResultValue,
} from './IOSNotifications';
import type {
IOSAttachment,
IOSAttachmentOptions,
NativeIOSNotification,
} from './types';
import { getLogger } from '../../utils/log';
import { getNativeModule } from '../../utils/native';
type CompletionHandler = BackgroundFetchResultValue => void;
export default class IOSNotification {
_alertAction: string | void;
@ -31,7 +38,13 @@ export default class IOSNotification {
_threadIdentifier: string | void; // N/A | threadIdentifier
constructor(notification: Notification, data?: NativeIOSNotification) {
_complete: CompletionHandler;
constructor(
notification: Notification,
notifications: IOSNotifications,
data?: NativeIOSNotification
) {
this._notification = notification;
if (data) {
@ -44,6 +57,20 @@ export default class IOSNotification {
this._threadIdentifier = data.threadIdentifier;
}
const complete = (fetchResult: BackgroundFetchResultValue) => {
const { notificationId } = notification;
getLogger(notifications).debug(
`Completion handler called for notificationId=${notificationId}`
);
getNativeModule(notifications).complete(notificationId, fetchResult);
};
if (notifications.shouldAutoComplete) {
complete(notifications.backgroundFetchResult.noData);
} else {
this._complete = complete;
}
// Defaults
this._attachments = this._attachments || [];
}

View File

@ -8,6 +8,7 @@ import IOSNotification from './IOSNotification';
import { generatePushID, isObject } from '../../utils';
import type { NativeNotification } from './types';
import type Notifications from '.';
export type NotificationOpen = {|
action: string,
@ -37,17 +38,27 @@ export default class Notification {
// N/A | subtitle | subText
_title: string; // alertTitle | title | contentTitle
constructor(data?: NativeNotification) {
this._android = new AndroidNotification(this, data && data.android);
this._ios = new IOSNotification(this, data && data.ios);
constructor(
nativeNotification?: NativeNotification,
notifications: Notifications
) {
this._android = new AndroidNotification(
this,
nativeNotification && nativeNotification.android
);
this._ios = new IOSNotification(
this,
notifications.ios,
nativeNotification && nativeNotification.ios
);
if (data) {
this._body = data.body;
this._data = data.data;
this._notificationId = data.notificationId;
this._sound = data.sound;
this._subtitle = data.subtitle;
this._title = data.title;
if (nativeNotification) {
this._body = nativeNotification.body;
this._data = nativeNotification.data;
this._notificationId = nativeNotification.notificationId;
this._sound = nativeNotification.sound;
this._subtitle = nativeNotification.subtitle;
this._title = nativeNotification.title;
}
// Defaults

View File

@ -97,29 +97,10 @@ export default class Notifications extends ModuleBase {
// public event name: onNotificationDisplayed
'notifications_notification_displayed',
(notification: NativeNotification) => {
const rnNotification = new Notification(notification);
const done = Platform.select({
ios: (fetchResult: string) => {
getLogger(this).debug(
`Completion handler called for notificationId=${
rnNotification.notificationId
}`
);
getNativeModule(this).complete(
rnNotification.notificationId,
fetchResult
);
},
android: () => {},
});
const publicEventName = 'onNotificationDisplayed';
if (this.ios.shouldAutoComplete) {
done(this.ios.backgroundFetchResult.noData);
}
SharedEventEmitter.emit(publicEventName, rnNotification, done);
SharedEventEmitter.emit(
'onNotificationDisplayed',
new Notification(notification, this)
);
}
);
@ -130,7 +111,7 @@ export default class Notifications extends ModuleBase {
(notificationOpen: NativeNotificationOpen) => {
SharedEventEmitter.emit('onNotificationOpened', {
action: notificationOpen.action,
notification: new Notification(notificationOpen.notification),
notification: new Notification(notificationOpen.notification, this),
results: notificationOpen.results,
});
}
@ -143,7 +124,7 @@ export default class Notifications extends ModuleBase {
(notification: NativeNotification) => {
SharedEventEmitter.emit(
'onNotification',
new Notification(notification)
new Notification(notification, this)
);
}
);
@ -215,7 +196,7 @@ export default class Notifications extends ModuleBase {
if (notificationOpen) {
return {
action: notificationOpen.action,
notification: new Notification(notificationOpen.notification),
notification: new Notification(notificationOpen.notification, this),
results: notificationOpen.results,
};
}