Commit a972d1ee authored by yogevbd's avatar yogevbd

Add typescript support

parent 7526f564
......@@ -189,4 +189,8 @@ package-lock.json
.history
android/.idea
android/build
# Typescript build
lib/dist/
android/.gradle
\ No newline at end of file
......@@ -7,24 +7,24 @@ import {
} from 'react-native';
import React, {Component} from 'react';
import NotificationsIOS, { NotificationAction, NotificationCategory } from 'react-native-notifications';
let upvoteAction = new NotificationAction({
activationMode: 'background',
title: String.fromCodePoint(0x1F44D),
identifier: 'UPVOTE_ACTION'
});
let replyAction = new NotificationAction({
activationMode: 'background',
title: 'Reply',
authenticationRequired: true,
textInput: {
buttonTitle: 'Reply now',
placeholder: 'Insert message'
},
identifier: 'REPLY_ACTION'
});
import { Notifications } from '../lib/dist/index';
// let upvoteAction = new NotificationAction({
// activationMode: 'background',
// title: String.fromCodePoint(0x1F44D),
// identifier: 'UPVOTE_ACTION'
// });
// let replyAction = new NotificationAction({
// activationMode: 'background',
// title: 'Reply',
// authenticationRequired: true,
// textInput: {
// buttonTitle: 'Reply now',
// placeholder: 'Insert message'
// },
// identifier: 'REPLY_ACTION'
// });
class NotificationsExampleApp extends Component {
......@@ -34,23 +34,25 @@ class NotificationsExampleApp extends Component {
notifications: []
};
NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this));
Notifications.events().registerNotificationsReceived((notification) => {
alert(JSON.stringify(notification));
})
// NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
// NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this));
NotificationsIOS.addEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
NotificationsIOS.registerPushKit();
// NotificationsIOS.addEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
// NotificationsIOS.registerPushKit();
NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.bind(this));
NotificationsIOS.addEventListener('pushKitNotificationReceived', this.onPushKitNotificationReceived.bind(this));
// NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
// NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.bind(this));
// NotificationsIOS.addEventListener('pushKitNotificationReceived', this.onPushKitNotificationReceived.bind(this));
}
async componentDidMount() {
const initialNotification = await NotificationsIOS.getInitialNotification();
const initialNotification = await Notifications.getInitialNotification();
if (initialNotification) {
this.setState({notifications: [initialNotification.getData().link, ...this.state.notifications]});
}
}
onPushRegistered(deviceToken) {
......@@ -109,15 +111,15 @@ class NotificationsExampleApp extends Component {
}
requestPermissions() {
let cat = new NotificationCategory({
identifier: 'SOME_CATEGORY',
actions: [upvoteAction, replyAction]
});
NotificationsIOS.requestPermissions([cat]);
// let cat = new NotificationCategory({
// identifier: 'SOME_CATEGORY',
// actions: [upvoteAction, replyAction]
// });
Notifications.requestPermissions(/*[cat]*/);
}
sendLocalNotification() {
NotificationsIOS.localNotification({
Notifications.localNotification({
body: 'Local notificiation!',
title: 'Local Notification Title',
sound: 'chime.aiff',
......@@ -127,14 +129,14 @@ class NotificationsExampleApp extends Component {
}
removeAllDeliveredNotifications() {
NotificationsIOS.removeAllDeliveredNotifications();
// NotificationsIOS.removeAllDeliveredNotifications();
}
componentWillUnmount() {
NotificationsIOS.removeEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
NotificationsIOS.removeEventListener('notificationOpened', this.onNotificationOpened.bind(this));
NotificationsIOS.removeEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
NotificationsIOS.removeEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
// NotificationsIOS.removeEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
// NotificationsIOS.removeEventListener('notificationOpened', this.onNotificationOpened.bind(this));
// NotificationsIOS.removeEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
// NotificationsIOS.removeEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
}
}
......
import { NativeCommandsSender } from './adapters/NativeCommandsSender';
import { NativeEventsReceiver } from './adapters/NativeEventsReceiver';
import { Commands } from './commands/Commands';
import { EventsRegistry } from './events/EventsRegistry';
import { Notification } from './interfaces/Notification';
export class NotificationsRoot {
private readonly nativeEventsReceiver: NativeEventsReceiver;
private readonly nativeCommandsSender: NativeCommandsSender;
private readonly commands: Commands;
private readonly eventsRegistry: EventsRegistry;
constructor() {
this.nativeEventsReceiver = new NativeEventsReceiver();
this.nativeCommandsSender = new NativeCommandsSender();
this.commands = new Commands(
this.nativeCommandsSender
);
this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver);
}
/**
* Request permissions to send remote notifications - iOS only
*/
public requestPermissions(): Promise<any> {
return this.commands.requestPermissions();
}
/**
* Reset the app to a new layout
*/
public localNotification(notification: Notification): Promise<any> {
return this.commands.sendLocalNotification(notification);
}
/**
*
*/
public getInitialNotification(): Promise<Notification> {
return this.commands.getInitialNotification();
}
/**
* Obtain the events registry instance
*/
public events(): EventsRegistry {
return this.eventsRegistry;
}
}
import { NativeModules } from 'react-native';
import { Notification } from '../interfaces/Notification';
interface NativeCommandsModule {
getInitialNotification(): Promise<any>;
localNotification(notification: Notification, id: string): Promise<Notification>;
requestPermissionsWithCategories(categories: any): Promise<any>;
}
export class NativeCommandsSender {
private readonly nativeCommandsModule: NativeCommandsModule;
constructor() {
this.nativeCommandsModule = NativeModules.RNBridgeModule;
}
sendLocalNotification(notification: Notification, id: string) {
return this.nativeCommandsModule.localNotification(notification, id);
}
getInitialNotification() {
return this.nativeCommandsModule.getInitialNotification();
}
requestPermissions() {
return this.nativeCommandsModule.requestPermissionsWithCategories([]);
}
}
import { NativeModules, NativeEventEmitter, EventEmitter, EmitterSubscription } from 'react-native';
import {
NotificationRegisteredEvent, NotificationReceived
} from '../interfaces/NotificationEvents';
export class NativeEventsReceiver {
private emitter: EventEmitter;
constructor() {
this.emitter = new NativeEventEmitter(NativeModules.RNEventEmitter);
}
public registerRemoteNotificationsRegistered(callback: (event: NotificationRegisteredEvent) => void): EmitterSubscription {
return this.emitter.addListener('remoteNotificationsRegistered', callback);
}
public registerRemoteNotificationsReceived(callback: (event: NotificationReceived) => void): EmitterSubscription {
return this.emitter.addListener('notificationReceivedForeground', callback);
}
}
import * as _ from 'lodash';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
import { Notification } from '../interfaces/Notification';
export class Commands {
constructor(
private readonly nativeCommandsSender: NativeCommandsSender
) {}
public sendLocalNotification(notification: Notification) {
const notificationId = 'id';
const result = this.nativeCommandsSender.sendLocalNotification(notification, notificationId);
return result;
}
public getInitialNotification() {
const result = this.nativeCommandsSender.getInitialNotification();
return result;
}
public requestPermissions() {
const result = this.nativeCommandsSender.requestPermissions();
return result;
}
}
import { EmitterSubscription } from 'react-native';
import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver';
import {
NotificationRegisteredEvent,
NotificationReceived
} from '../interfaces/NotificationEvents';
export class EventsRegistry {
constructor(private nativeEventsReceiver: NativeEventsReceiver) { }
public registerRemoteNotificationsRegistered(callback: (event: NotificationRegisteredEvent) => void): EmitterSubscription {
return this.nativeEventsReceiver.registerRemoteNotificationsRegistered(callback);
}
public registerNotificationsReceived(callback: (event: NotificationReceived) => void): EmitterSubscription {
return this.nativeEventsReceiver.registerRemoteNotificationsReceived(callback);
}
}
import { NotificationsRoot } from './Notifications';
const notificationsSingleton = new NotificationsRoot();
export const Notifications = notificationsSingleton;
export * from './interfaces/EventSubscription';
export * from './interfaces/Notification';
export * from './interfaces/NotificationEvents';
export interface EventSubscription {
remove(): void;
}
export interface Notification {
data: any;
alert: string
sound: string;
badge: number;
type: string;
thread: string;
}
import { Notification } from './Notification';
export interface NotificationRegisteredEvent {
deviceToken: string;
}
export interface NotificationReceived {
notification: Notification;
}
......@@ -3,7 +3,11 @@ module.exports = {
watchFolders: [
__dirname,
],
resolver: {
sourceExts: ['ts', 'tsx', 'js']
},
transformer: {
babelTransformerPath: require.resolve('react-native-typescript-transformer'),
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
......
......@@ -19,8 +19,11 @@
"actionable-notifications",
"interactive-notifications"
],
"main": "lib/src/index",
"main": "lib/dist/index.js",
"typings": "lib/dist/index.d.ts",
"scripts": {
"build": "rm -rf ./lib/dist && tsc",
"prestart": "npm run build",
"pretest": "./node_modules/.bin/eslint *.js test",
"test": "node scripts/test",
"start": "node ./scripts/start",
......@@ -45,11 +48,13 @@
"@types/react-test-renderer": "16.x.x",
"@babel/plugin-proposal-export-default-from": "7.2.0",
"@babel/plugin-proposal-export-namespace-from": "7.2.0",
"@types/jest": "23.x.x",
"@types/lodash": "4.x.x",
"typescript": "3.2.2",
"babel-eslint": "9.0.0",
"chai": "^3.5.0",
"chokidar-cli": "^1.2.0",
"eslint": "6.0.1",
"tslint": "5.x.x",
"mocha": "^2.5.3",
"proxyquire": "^1.7.4",
"sinon": "^1.17.3",
......@@ -60,7 +65,8 @@
"detox": "13.x.x",
"jsc-android": "236355.x.x",
"jest": "24.8.0",
"metro-react-native-babel-preset": "0.55.x"
"metro-react-native-babel-preset": "0.55.x",
"react-native-typescript-transformer": "1.2.12"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
......
{
"$schema": "http://json.schemastore.org/tsconfig",
"compilerOptions": {
"allowSyntheticDefaultImports": false,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"diagnostics": true,
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"pretty": true,
"strict": true
}
}
{
"extends": "./tsconfig-strict.json",
"compilerOptions": {
"outDir": "./lib/dist",
"allowJs": false,
"target": "esnext",
"module": "commonjs",
"jsx": "react-native",
"declaration": true,
"skipLibCheck": true,
"types": [
"jest",
"lodash",
"react",
"react-native",
"react-test-renderer"
]
},
"include": [
"./lib/src/**/*"
]
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment