Commit a972d1ee authored by yogevbd's avatar yogevbd

Add typescript support

parent 7526f564
...@@ -189,4 +189,8 @@ package-lock.json ...@@ -189,4 +189,8 @@ package-lock.json
.history .history
android/.idea android/.idea
android/build android/build
# Typescript build
lib/dist/
android/.gradle android/.gradle
\ No newline at end of file
...@@ -7,24 +7,24 @@ import { ...@@ -7,24 +7,24 @@ import {
} from 'react-native'; } from 'react-native';
import React, {Component} from 'react'; import React, {Component} from 'react';
import NotificationsIOS, { NotificationAction, NotificationCategory } from 'react-native-notifications'; import { Notifications } from '../lib/dist/index';
let upvoteAction = new NotificationAction({ // let upvoteAction = new NotificationAction({
activationMode: 'background', // activationMode: 'background',
title: String.fromCodePoint(0x1F44D), // title: String.fromCodePoint(0x1F44D),
identifier: 'UPVOTE_ACTION' // identifier: 'UPVOTE_ACTION'
}); // });
let replyAction = new NotificationAction({ // let replyAction = new NotificationAction({
activationMode: 'background', // activationMode: 'background',
title: 'Reply', // title: 'Reply',
authenticationRequired: true, // authenticationRequired: true,
textInput: { // textInput: {
buttonTitle: 'Reply now', // buttonTitle: 'Reply now',
placeholder: 'Insert message' // placeholder: 'Insert message'
}, // },
identifier: 'REPLY_ACTION' // identifier: 'REPLY_ACTION'
}); // });
class NotificationsExampleApp extends Component { class NotificationsExampleApp extends Component {
...@@ -34,23 +34,25 @@ class NotificationsExampleApp extends Component { ...@@ -34,23 +34,25 @@ class NotificationsExampleApp extends Component {
notifications: [] notifications: []
}; };
NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this)); Notifications.events().registerNotificationsReceived((notification) => {
NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this)); 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.addEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
NotificationsIOS.registerPushKit(); // NotificationsIOS.registerPushKit();
NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this)); // NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.bind(this)); // NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.bind(this));
NotificationsIOS.addEventListener('pushKitNotificationReceived', this.onPushKitNotificationReceived.bind(this)); // NotificationsIOS.addEventListener('pushKitNotificationReceived', this.onPushKitNotificationReceived.bind(this));
} }
async componentDidMount() { async componentDidMount() {
const initialNotification = await NotificationsIOS.getInitialNotification(); const initialNotification = await Notifications.getInitialNotification();
if (initialNotification) { if (initialNotification) {
this.setState({notifications: [initialNotification.getData().link, ...this.state.notifications]}); this.setState({notifications: [initialNotification.getData().link, ...this.state.notifications]});
} }
} }
onPushRegistered(deviceToken) { onPushRegistered(deviceToken) {
...@@ -109,15 +111,15 @@ class NotificationsExampleApp extends Component { ...@@ -109,15 +111,15 @@ class NotificationsExampleApp extends Component {
} }
requestPermissions() { requestPermissions() {
let cat = new NotificationCategory({ // let cat = new NotificationCategory({
identifier: 'SOME_CATEGORY', // identifier: 'SOME_CATEGORY',
actions: [upvoteAction, replyAction] // actions: [upvoteAction, replyAction]
}); // });
NotificationsIOS.requestPermissions([cat]); Notifications.requestPermissions(/*[cat]*/);
} }
sendLocalNotification() { sendLocalNotification() {
NotificationsIOS.localNotification({ Notifications.localNotification({
body: 'Local notificiation!', body: 'Local notificiation!',
title: 'Local Notification Title', title: 'Local Notification Title',
sound: 'chime.aiff', sound: 'chime.aiff',
...@@ -127,14 +129,14 @@ class NotificationsExampleApp extends Component { ...@@ -127,14 +129,14 @@ class NotificationsExampleApp extends Component {
} }
removeAllDeliveredNotifications() { removeAllDeliveredNotifications() {
NotificationsIOS.removeAllDeliveredNotifications(); // NotificationsIOS.removeAllDeliveredNotifications();
} }
componentWillUnmount() { componentWillUnmount() {
NotificationsIOS.removeEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this)); // NotificationsIOS.removeEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
NotificationsIOS.removeEventListener('notificationOpened', this.onNotificationOpened.bind(this)); // NotificationsIOS.removeEventListener('notificationOpened', this.onNotificationOpened.bind(this));
NotificationsIOS.removeEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this)); // NotificationsIOS.removeEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
NotificationsIOS.removeEventListener('pushKitRegistered', this.onPushKitRegistered.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 = { ...@@ -3,7 +3,11 @@ module.exports = {
watchFolders: [ watchFolders: [
__dirname, __dirname,
], ],
resolver: {
sourceExts: ['ts', 'tsx', 'js']
},
transformer: { transformer: {
babelTransformerPath: require.resolve('react-native-typescript-transformer'),
getTransformOptions: async () => ({ getTransformOptions: async () => ({
transform: { transform: {
experimentalImportSupport: false, experimentalImportSupport: false,
......
...@@ -19,8 +19,11 @@ ...@@ -19,8 +19,11 @@
"actionable-notifications", "actionable-notifications",
"interactive-notifications" "interactive-notifications"
], ],
"main": "lib/src/index", "main": "lib/dist/index.js",
"typings": "lib/dist/index.d.ts",
"scripts": { "scripts": {
"build": "rm -rf ./lib/dist && tsc",
"prestart": "npm run build",
"pretest": "./node_modules/.bin/eslint *.js test", "pretest": "./node_modules/.bin/eslint *.js test",
"test": "node scripts/test", "test": "node scripts/test",
"start": "node ./scripts/start", "start": "node ./scripts/start",
...@@ -45,11 +48,13 @@ ...@@ -45,11 +48,13 @@
"@types/react-test-renderer": "16.x.x", "@types/react-test-renderer": "16.x.x",
"@babel/plugin-proposal-export-default-from": "7.2.0", "@babel/plugin-proposal-export-default-from": "7.2.0",
"@babel/plugin-proposal-export-namespace-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", "typescript": "3.2.2",
"babel-eslint": "9.0.0", "babel-eslint": "9.0.0",
"chai": "^3.5.0", "chai": "^3.5.0",
"chokidar-cli": "^1.2.0", "chokidar-cli": "^1.2.0",
"eslint": "6.0.1", "tslint": "5.x.x",
"mocha": "^2.5.3", "mocha": "^2.5.3",
"proxyquire": "^1.7.4", "proxyquire": "^1.7.4",
"sinon": "^1.17.3", "sinon": "^1.17.3",
...@@ -60,7 +65,8 @@ ...@@ -60,7 +65,8 @@
"detox": "13.x.x", "detox": "13.x.x",
"jsc-android": "236355.x.x", "jsc-android": "236355.x.x",
"jest": "24.8.0", "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": { "publishConfig": {
"registry": "https://registry.npmjs.org/" "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