2018-02-09 17:00:03 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Notification representation wrapper
|
|
|
|
*/
|
2018-02-16 09:46:53 +00:00
|
|
|
import { Platform } from 'react-native';
|
2018-02-09 17:00:03 +00:00
|
|
|
import AndroidNotification from './AndroidNotification';
|
|
|
|
import IOSNotification from './IOSNotification';
|
2018-02-15 14:59:21 +00:00
|
|
|
import { generatePushID, isObject } from '../../utils';
|
2018-02-09 17:00:03 +00:00
|
|
|
|
2018-02-22 15:52:24 +00:00
|
|
|
import type { NativeNotification } from './types';
|
2018-02-09 17:00:03 +00:00
|
|
|
|
2018-03-09 11:09:20 +00:00
|
|
|
export type NotificationOpen = {|
|
2018-02-22 17:25:15 +00:00
|
|
|
action: string,
|
|
|
|
notification: Notification,
|
2018-03-09 11:09:20 +00:00
|
|
|
results?: { [string]: string },
|
|
|
|
|};
|
2018-02-22 17:25:15 +00:00
|
|
|
|
2018-02-09 17:00:03 +00:00
|
|
|
export default class Notification {
|
|
|
|
// iOS 8/9 | 10+ | Android
|
|
|
|
_android: AndroidNotification;
|
|
|
|
_body: string; // alertBody | body | contentText
|
|
|
|
_data: { [string]: string }; // userInfo | userInfo | extras
|
|
|
|
_ios: IOSNotification;
|
2018-02-15 14:59:21 +00:00
|
|
|
_notificationId: string;
|
2018-02-09 17:00:03 +00:00
|
|
|
_sound: string | void; // soundName | sound | sound
|
|
|
|
_subtitle: string | void; // N/A | subtitle | subText
|
|
|
|
_title: string; // alertTitle | title | contentTitle
|
|
|
|
|
2018-02-22 15:52:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
this._data = this._data || {};
|
2018-02-23 16:11:59 +00:00
|
|
|
// TODO: Is this the best way to generate an ID?
|
2018-02-22 15:52:24 +00:00
|
|
|
this._notificationId = this._notificationId || generatePushID();
|
2018-02-09 17:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get android(): AndroidNotification {
|
|
|
|
return this._android;
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:11:59 +00:00
|
|
|
get body(): string {
|
|
|
|
return this._body;
|
|
|
|
}
|
|
|
|
|
|
|
|
get data(): { [string]: string } {
|
|
|
|
return this._data;
|
|
|
|
}
|
|
|
|
|
2018-02-09 17:00:03 +00:00
|
|
|
get ios(): IOSNotification {
|
|
|
|
return this._ios;
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:11:59 +00:00
|
|
|
get notificationId(): string {
|
|
|
|
return this._notificationId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get sound(): ?string {
|
|
|
|
return this._sound;
|
|
|
|
}
|
|
|
|
|
|
|
|
get subtitle(): ?string {
|
|
|
|
return this._subtitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
get title(): string {
|
|
|
|
return this._title;
|
|
|
|
}
|
|
|
|
|
2018-02-09 17:00:03 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param body
|
|
|
|
* @returns {Notification}
|
|
|
|
*/
|
|
|
|
setBody(body: string): Notification {
|
|
|
|
this._body = body;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* @returns {Notification}
|
|
|
|
*/
|
|
|
|
setData(data: Object = {}): Notification {
|
|
|
|
if (!isObject(data)) {
|
|
|
|
throw new Error(
|
|
|
|
`Notification:withData expects an object but got type '${typeof data}'.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this._data = data;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:59:21 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param notificationId
|
|
|
|
* @returns {Notification}
|
|
|
|
*/
|
|
|
|
setNotificationId(notificationId: string): Notification {
|
|
|
|
this._notificationId = notificationId;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-02-09 17:00:03 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param sound
|
|
|
|
* @returns {Notification}
|
|
|
|
*/
|
|
|
|
setSound(sound: string): Notification {
|
|
|
|
this._sound = sound;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param subtitle
|
|
|
|
* @returns {Notification}
|
|
|
|
*/
|
|
|
|
setSubtitle(subtitle: string): Notification {
|
|
|
|
this._subtitle = subtitle;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param title
|
|
|
|
* @returns {Notification}
|
|
|
|
*/
|
|
|
|
setTitle(title: string): Notification {
|
|
|
|
this._title = title;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
build(): NativeNotification {
|
|
|
|
// Android required fields: body, title, smallicon
|
|
|
|
// iOS required fields: TODO
|
2018-02-15 14:59:21 +00:00
|
|
|
if (!this._body) {
|
2018-02-09 17:00:03 +00:00
|
|
|
throw new Error('Notification: Missing required `body` property');
|
2018-02-15 14:59:21 +00:00
|
|
|
} else if (!this._notificationId) {
|
|
|
|
throw new Error(
|
|
|
|
'Notification: Missing required `notificationId` property'
|
|
|
|
);
|
|
|
|
} else if (!this._title) {
|
2018-02-09 17:00:03 +00:00
|
|
|
throw new Error('Notification: Missing required `title` property');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2018-02-16 09:46:53 +00:00
|
|
|
android: Platform.OS === 'android' ? this._android.build() : undefined,
|
2018-02-09 17:00:03 +00:00
|
|
|
body: this._body,
|
|
|
|
data: this._data,
|
2018-02-16 09:46:53 +00:00
|
|
|
ios: Platform.OS === 'ios' ? this._ios.build() : undefined,
|
2018-02-15 14:59:21 +00:00
|
|
|
notificationId: this._notificationId,
|
2018-02-09 17:00:03 +00:00
|
|
|
sound: this._sound,
|
|
|
|
subtitle: this._subtitle,
|
|
|
|
title: this._title,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|