2
0
mirror of synced 2025-02-22 11:08:28 +00:00

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 * IOSNotification representation wrapper
*/ */
import type Notification from './Notification'; import type Notification from './Notification';
import IOSNotifications, {
type BackgroundFetchResultValue,
} from './IOSNotifications';
import type { import type {
IOSAttachment, IOSAttachment,
IOSAttachmentOptions, IOSAttachmentOptions,
NativeIOSNotification, NativeIOSNotification,
} from './types'; } from './types';
import { getLogger } from '../../utils/log';
import { getNativeModule } from '../../utils/native';
type CompletionHandler = BackgroundFetchResultValue => void;
export default class IOSNotification { export default class IOSNotification {
_alertAction: string | void; _alertAction: string | void;
@ -31,7 +38,13 @@ export default class IOSNotification {
_threadIdentifier: string | void; // N/A | threadIdentifier _threadIdentifier: string | void; // N/A | threadIdentifier
constructor(notification: Notification, data?: NativeIOSNotification) { _complete: CompletionHandler;
constructor(
notification: Notification,
notifications: IOSNotifications,
data?: NativeIOSNotification
) {
this._notification = notification; this._notification = notification;
if (data) { if (data) {
@ -44,6 +57,20 @@ export default class IOSNotification {
this._threadIdentifier = data.threadIdentifier; 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 // Defaults
this._attachments = this._attachments || []; this._attachments = this._attachments || [];
} }

View File

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

View File

@ -97,29 +97,10 @@ export default class Notifications extends ModuleBase {
// public event name: onNotificationDisplayed // public event name: onNotificationDisplayed
'notifications_notification_displayed', 'notifications_notification_displayed',
(notification: NativeNotification) => { (notification: NativeNotification) => {
const rnNotification = new Notification(notification); SharedEventEmitter.emit(
const done = Platform.select({ 'onNotificationDisplayed',
ios: (fetchResult: string) => { new Notification(notification, this)
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);
} }
); );
@ -130,7 +111,7 @@ export default class Notifications extends ModuleBase {
(notificationOpen: NativeNotificationOpen) => { (notificationOpen: NativeNotificationOpen) => {
SharedEventEmitter.emit('onNotificationOpened', { SharedEventEmitter.emit('onNotificationOpened', {
action: notificationOpen.action, action: notificationOpen.action,
notification: new Notification(notificationOpen.notification), notification: new Notification(notificationOpen.notification, this),
results: notificationOpen.results, results: notificationOpen.results,
}); });
} }
@ -143,7 +124,7 @@ export default class Notifications extends ModuleBase {
(notification: NativeNotification) => { (notification: NativeNotification) => {
SharedEventEmitter.emit( SharedEventEmitter.emit(
'onNotification', 'onNotification',
new Notification(notification) new Notification(notification, this)
); );
} }
); );
@ -215,7 +196,7 @@ export default class Notifications extends ModuleBase {
if (notificationOpen) { if (notificationOpen) {
return { return {
action: notificationOpen.action, action: notificationOpen.action,
notification: new Notification(notificationOpen.notification), notification: new Notification(notificationOpen.notification, this),
results: notificationOpen.results, results: notificationOpen.results,
}; };
} }