Merge pull request #1393 from ryanggrey/background-notification-fix-ios

Background notification fix for ios
This commit is contained in:
Michael Diarmid
2018-08-16 12:46:20 +01:00
committed by GitHub
9 changed files with 154 additions and 20 deletions

4
src/index.d.ts vendored
View File

@@ -1141,6 +1141,9 @@ declare module 'react-native-firebase' {
deleteChannel(channelId: string): Promise<void>;
}
type BackgroundFetchResultValue = string;
type CompletionHandler = (backgroundFetchResult: BackgroundFetchResultValue) => void;
interface Notifications {
android: AndroidNotifications;
@@ -1475,6 +1478,7 @@ declare module 'react-native-firebase' {
hasAction?: boolean;
launchImage?: string;
threadIdentifier?: string;
complete?: CompletionHandler;
addAttachment(
identifier: string,

View File

@@ -3,11 +3,17 @@
* IOSNotification representation wrapper
*/
import type Notification from './Notification';
import type Notifications from '.';
import { 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 +37,13 @@ export default class IOSNotification {
_threadIdentifier: string | void; // N/A | threadIdentifier
constructor(notification: Notification, data?: NativeIOSNotification) {
_complete: CompletionHandler;
constructor(
notification: Notification,
notifications: Notifications,
data?: NativeIOSNotification
) {
this._notification = notification;
if (data) {
@@ -44,6 +56,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.ios.shouldAutoComplete) {
complete(notifications.ios.backgroundFetchResult.noData);
} else {
this._complete = complete;
}
// Defaults
this._attachments = this._attachments || [];
}
@@ -76,6 +102,10 @@ export default class IOSNotification {
return this._threadIdentifier;
}
get complete(): CompletionHandler {
return this._complete;
}
/**
*
* @param identifier

View File

@@ -0,0 +1,31 @@
import { getNativeModule } from '../../utils/native';
import type Notifications from '.';
export type BackgroundFetchResultValue = string;
type BackgroundFetchResult = {
noData: BackgroundFetchResultValue,
newData: BackgroundFetchResultValue,
failure: BackgroundFetchResultValue,
};
export default class IOSNotifications {
_backgroundFetchResult: BackgroundFetchResult;
shouldAutoComplete: boolean;
constructor(notifications: Notifications) {
this.shouldAutoComplete = true;
const nativeModule = getNativeModule(notifications);
this._backgroundFetchResult = {
noData: nativeModule.backgroundFetchResultNoData,
newData: nativeModule.backgroundFetchResultNewData,
failure: nativeModule.backgroundFetchResultFailure,
};
}
get backgroundFetchResult(): BackgroundFetchResult {
return { ...this._backgroundFetchResult };
}
}

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,19 +38,29 @@ 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);
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;
constructor(
nativeNotification?: NativeNotification,
notifications: Notifications
) {
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;
}
this._android = new AndroidNotification(
this,
nativeNotification && nativeNotification.android
);
this._ios = new IOSNotification(
this,
notifications,
nativeNotification && nativeNotification.ios
);
// Defaults
this._data = this._data || {};
// TODO: Is this the best way to generate an ID?

View File

@@ -12,6 +12,7 @@ import AndroidAction from './AndroidAction';
import AndroidChannel from './AndroidChannel';
import AndroidChannelGroup from './AndroidChannelGroup';
import AndroidNotifications from './AndroidNotifications';
import IOSNotifications from './IOSNotifications';
import AndroidRemoteInput from './AndroidRemoteInput';
import Notification from './Notification';
import {
@@ -74,6 +75,8 @@ export const NAMESPACE = 'notifications';
export default class Notifications extends ModuleBase {
_android: AndroidNotifications;
_ios: IOSNotifications;
constructor(app: App) {
super(app, {
events: NATIVE_EVENTS,
@@ -83,6 +86,7 @@ export default class Notifications extends ModuleBase {
namespace: NAMESPACE,
});
this._android = new AndroidNotifications(this);
this._ios = new IOSNotifications(this);
SharedEventEmitter.addListener(
// sub to internal native event - this fans out to
@@ -91,7 +95,7 @@ export default class Notifications extends ModuleBase {
(notification: NativeNotification) => {
SharedEventEmitter.emit(
'onNotificationDisplayed',
new Notification(notification)
new Notification(notification, this)
);
}
);
@@ -103,7 +107,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,
});
}
@@ -116,7 +120,7 @@ export default class Notifications extends ModuleBase {
(notification: NativeNotification) => {
SharedEventEmitter.emit(
'onNotification',
new Notification(notification)
new Notification(notification, this)
);
}
);
@@ -131,6 +135,10 @@ export default class Notifications extends ModuleBase {
return this._android;
}
get ios(): IOSNotifications {
return this._ios;
}
/**
* Cancel all notifications
*/
@@ -184,7 +192,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,
};
}