diff --git a/native/push/comm-ios-notification.js b/native/push/comm-ios-notification.js new file mode 100644 --- /dev/null +++ b/native/push/comm-ios-notification.js @@ -0,0 +1,52 @@ +// @flow +import { NativeModules } from 'react-native'; + +const { CommIOSNotifications } = NativeModules; + +export type RawIOSNotification = { + +id: ?string, + +aps: { 'content-available': ?number, 'alert': ?string, ... }, + +managedAps: ?{ action: string, alert: ?string, ... }, + ... +}; + +export class CommIOSNotification { + _data: Object; + _alert: ?string; + _id: ?string; + _remoteNotificationCompleteCallbackCalled: boolean; + + constructor(notification: RawIOSNotification) { + const { aps, ...coreNotifData } = notification; + this._data = coreNotifData; + this._id = notification.id; + this._remoteNotificationCompleteCallbackCalled = false; + + if ( + notification.aps && + notification.aps['content-available'] && + notification.aps['content-available'] === 1 && + notification.managedAps + ) { + this._alert = notification.managedAps.alert; + } else if (notification.aps && notification.aps.alert) { + this._alert = notification.aps.alert; + } + } + + getMessage(): ?string { + return this._alert; + } + + getData(): Object { + return this._data; + } + + finish(fetchResult: string) { + if (!this._id || this._remoteNotificationCompleteCallbackCalled) { + return; + } + this._remoteNotificationCompleteCallbackCalled = true; + CommIOSNotifications.completeNotif(this._id, fetchResult); + } +}