[notifications] Continue android implementation

This commit is contained in:
Chris Bianca
2018-02-15 14:59:21 +00:00
parent 99b4b6550b
commit cd0ef4e3b7
15 changed files with 413 additions and 276 deletions

View File

@@ -5,7 +5,7 @@
import ModuleBase from '../../utils/ModuleBase';
import { getNativeModule } from '../../utils/native';
import type App from '../core/firebase-app';
import type App from '../core/app';
export const MODULE_NAME = 'RNFirebaseInstanceId';
export const NAMESPACE = 'instanceid';

View File

@@ -17,17 +17,18 @@ type Progress = {
};
type SmallIcon = {
icon: number,
icon: string,
level?: number,
};
export type NativeAndroidNotification = {
export type NativeAndroidNotification = {|
// TODO actions: Action[],
autoCancel: boolean,
badgeIconType: BadgeIconTypeType,
category: CategoryType,
channelId: string,
color: number,
clickAction?: string,
color: string,
colorized: boolean,
contentInfo: string,
defaults: DefaultsType[],
@@ -43,7 +44,7 @@ export type NativeAndroidNotification = {
people: string[],
priority: PriorityType,
progress: Progress,
publicVersion: Notification,
// publicVersion: Notification,
remoteInputHistory: string[],
shortcutId: string,
showWhen: boolean,
@@ -56,7 +57,7 @@ export type NativeAndroidNotification = {
vibrate: number[],
visibility: VisibilityType,
when: number,
};
|};
export const BadgeIconType = {
Large: 2,
@@ -122,7 +123,8 @@ export default class AndroidNotification {
_badgeIconType: BadgeIconTypeType;
_category: CategoryType;
_channelId: string;
_color: number;
_clickAction: string;
_color: string;
_colorized: boolean;
_contentInfo: string;
_defaults: DefaultsType[];
@@ -139,11 +141,13 @@ export default class AndroidNotification {
_people: string[];
_priority: PriorityType;
_progress: Progress;
_publicVersion: Notification;
// _publicVersion: Notification;
_remoteInputHistory: string[];
_shortcutId: string;
_showWhen: boolean;
_smallIcon: SmallIcon;
_smallIcon: SmallIcon = {
icon: 'ic_launcher',
};
_sortKey: string;
// TODO: style: Style; // Need to figure out if this can work
_ticker: string;
@@ -170,9 +174,7 @@ export default class AndroidNotification {
/**
*
* @param identifier
* @param identifier
* @param identifier
* @param person
* @returns {Notification}
*/
addPerson(person: string): Notification {
@@ -228,7 +230,7 @@ export default class AndroidNotification {
* @param color
* @returns {Notification}
*/
setColor(color: number): Notification {
setColor(color: string): Notification {
this._color = color;
return this._notification;
}
@@ -394,10 +396,10 @@ export default class AndroidNotification {
* @param publicVersion
* @returns {Notification}
*/
setPublicVersion(publicVersion: Notification): Notification {
/* setPublicVersion(publicVersion: Notification): Notification {
this._publicVersion = publicVersion;
return this._notification;
}
} */
/**
*
@@ -435,7 +437,7 @@ export default class AndroidNotification {
* @param level
* @returns {Notification}
*/
setSmallIcon(icon: number, level?: number): Notification {
setSmallIcon(icon: string, level?: number): Notification {
this._smallIcon = {
icon,
level,
@@ -517,6 +519,7 @@ export default class AndroidNotification {
badgeIconType: this._badgeIconType,
category: this._category,
channelId: this._channelId,
clickAction: this._clickAction,
color: this._color,
colorized: this._colorized,
contentInfo: this._contentInfo,
@@ -533,7 +536,7 @@ export default class AndroidNotification {
people: this._people,
priority: this._priority,
progress: this._progress,
publicVersion: this._publicVersion,
// publicVersion: this._publicVersion,
remoteInputHistory: this._remoteInputHistory,
shortcutId: this._shortcutId,
showWhen: this._showWhen,

View File

@@ -2,7 +2,6 @@
* @flow
* IOSNotification representation wrapper
*/
import { generatePushID } from '../../utils';
import type Notification from './Notification';
type AttachmentOptions = {|
@@ -23,16 +22,15 @@ type Attachment = {|
url: string,
|};
export type NativeIOSNotification = {
export type NativeIOSNotification = {|
alertAction?: string,
attachments: Attachment[],
badge?: number,
category?: string,
hasAction?: boolean,
identifier?: string,
launchImage?: string,
threadIdentifier?: string,
};
|};
export default class IOSNotification {
_alertAction: string; // alertAction | N/A
@@ -40,23 +38,20 @@ export default class IOSNotification {
_badge: number; // applicationIconBadgeNumber | badge
_category: string;
_hasAction: boolean; // hasAction | N/A
_identifier: string; // N/A | identifier
_launchImage: string; // alertLaunchImage | launchImageName
_notification: Notification;
_threadIdentifier: string; // N/A | threadIdentifier
constructor(notification: Notification) {
this._attachments = [];
// TODO: Is this the best way to generate an ID?
this._identifier = generatePushID();
this._notification = notification;
}
/**
*
* @param identifier
* @param identifier
* @param identifier
* @param url
* @param options
* @returns {Notification}
*/
addAttachment(
@@ -112,16 +107,6 @@ export default class IOSNotification {
return this._notification;
}
/**
*
* @param identifier
* @returns {Notification}
*/
setIdentifier(identifier: string): Notification {
this._identifier = identifier;
return this._notification;
}
/**
*
* @param launchImage
@@ -151,7 +136,6 @@ export default class IOSNotification {
badge: this._badge,
category: this._category,
hasAction: this._hasAction,
identifier: this._identifier,
launchImage: this._launchImage,
threadIdentifier: this._threadIdentifier,
};

View File

@@ -4,16 +4,19 @@
*/
import AndroidNotification from './AndroidNotification';
import IOSNotification from './IOSNotification';
import { isObject } from '../../utils';
import { generatePushID, isObject } from '../../utils';
import type { NativeAndroidNotification } from './AndroidNotification';
import type { NativeIOSNotification } from './IOSNotification';
import type { Schedule } from './';
type NativeNotification = {|
android: NativeAndroidNotification,
body: string,
data: { [string]: string },
ios: NativeIOSNotification,
notificationId: string,
schedule?: Schedule,
sound?: string,
subtitle?: string,
title: string,
@@ -25,6 +28,7 @@ export default class Notification {
_body: string; // alertBody | body | contentText
_data: { [string]: string }; // userInfo | userInfo | extras
_ios: IOSNotification;
_notificationId: string;
_sound: string | void; // soundName | sound | sound
_subtitle: string | void; // N/A | subtitle | subText
_title: string; // alertTitle | title | contentTitle
@@ -33,6 +37,8 @@ export default class Notification {
this._android = new AndroidNotification(this);
this._data = {};
this._ios = new IOSNotification(this);
// TODO: Is this the best way to generate an ID?
this._notificationId = generatePushID();
}
get android(): AndroidNotification {
@@ -68,6 +74,16 @@ export default class Notification {
return this;
}
/**
*
* @param notificationId
* @returns {Notification}
*/
setNotificationId(notificationId: string): Notification {
this._notificationId = notificationId;
return this;
}
/**
*
* @param sound
@@ -101,9 +117,13 @@ export default class Notification {
build(): NativeNotification {
// Android required fields: body, title, smallicon
// iOS required fields: TODO
if (!this.body) {
if (!this._body) {
throw new Error('Notification: Missing required `body` property');
} else if (!this.title) {
} else if (!this._notificationId) {
throw new Error(
'Notification: Missing required `notificationId` property'
);
} else if (!this._title) {
throw new Error('Notification: Missing required `title` property');
}
@@ -112,6 +132,7 @@ export default class Notification {
body: this._body,
data: this._data,
ios: this._ios.build(),
notificationId: this._notificationId,
sound: this._sound,
subtitle: this._subtitle,
title: this._title,

View File

@@ -17,7 +17,7 @@ import {
Visibility,
} from './AndroidNotification';
import type App from '../core/firebase-app';
import type App from '../core/app';
// TODO: Received notification type will be different from sent notification
type OnNotification = Notification => any;
@@ -26,9 +26,10 @@ type OnNotificationObserver = {
next: OnNotification,
};
// TODO: Schedule type
type Schedule = {
build: () => Object,
export type Schedule = {
exact?: boolean,
fireDate: number,
repeatInterval?: 'minute' | 'hour' | 'day' | 'week',
};
const NATIVE_EVENTS = ['notifications_notification_received'];
@@ -174,10 +175,9 @@ export default class Notifications extends ModuleBase {
`Notifications:scheduleNotification expects a 'Notification' but got type ${typeof notification}`
);
}
return getNativeModule(this).scheduleNotification(
notification.build(),
schedule.build()
);
const nativeNotification = notification.build();
nativeNotification.schedule = schedule;
return getNativeModule(this).scheduleNotification(nativeNotification);
}
}