Commit 4e661458 authored by yogevbd's avatar yogevbd

Split Notification.ts, Fix android example module

parent 36a833fc
......@@ -13,10 +13,9 @@ apply from: "../../../node_modules/react-native/react.gradle"
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.wix.reactnativenotifications.app"
minSdkVersion 19
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
......
package com.wix.reactnativenotifications.app;
import android.os.Build;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Toolbar;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactRootView;
import static android.os.Build.VERSION.SDK_INT;
public class MainActivity extends ReactActivity {
private ReactRootView mReactRootView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewGroup layout;
if (SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
layout = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_main, null);
Toolbar toolbar = layout.findViewById(R.id.toolbar);
setActionBar(toolbar);
} else {
layout = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_main_prelollipop, null);
}
mReactRootView = new ReactRootView(this);
layout.addView(mReactRootView);
setContentView(layout);
startReactApplication();
}
private void startReactApplication() {
mReactRootView.startReactApplication(getReactInstanceManager(), "NotificationsExampleApp", null);
@Override
protected String getMainComponentName() {
return "NotificationsExampleApp";
}
}
......@@ -24,7 +24,12 @@ public class MainApplication extends Application implements ReactApplication {
return Arrays.asList(
new MainReactPackage(),
new RNNotificationsPackage(MainApplication.this)
);
);
}
@Override
protected String getJSMainModuleName() {
return "index";
}
};
......
......@@ -6,7 +6,7 @@ import {
Button
} from 'react-native';
import React, {Component} from 'react';
import { Notifications } from 'react-native-notifications';
import {Notifications} from 'react-native-notifications';
class NotificationsExampleApp extends Component {
constructor() {
......@@ -22,7 +22,7 @@ class NotificationsExampleApp extends Component {
registerNotificationEvents() {
Notifications.events().registerNotificationReceived((notification, completion) => {
this.setState({
notifications: [...this.state.notifications, notification.link]
notifications: [...this.state.notifications, notification]
});
completion({alert: true, sound: false, badge: false});
......@@ -30,15 +30,21 @@ class NotificationsExampleApp extends Component {
Notifications.events().registerRemoteNotificationOpened((notification, completion) => {
this.setState({
notifications: [...this.state.notifications, `Notification Clicked: ${notification.link}`]
notifications: [...this.state.notifications, notification]
});
completion();
});
}
renderNotification(notification) {
return <Text>{`${notification}`}</Text>;
return (
<View style={{backgroundColor: 'lightgray', margin: 10}}>
<Text>{`Title: ${notification.title}`}</Text>
<Text>{`Body: ${notification.body}`}</Text>
<Text>{`Extra Link Param: ${notification.data.link}`}</Text>
</View>
);
}
requestPermissions() {
......@@ -51,7 +57,7 @@ class NotificationsExampleApp extends Component {
title: String.fromCodePoint(0x1F44D),
identifier: 'UPVOTE_ACTION'
};
const replyAction = {
activationMode: 'background',
title: 'Reply',
......@@ -62,7 +68,7 @@ class NotificationsExampleApp extends Component {
},
identifier: 'REPLY_ACTION'
};
const category = {
identifier: 'SOME_CATEGORY',
actions: [upvoteAction, replyAction]
......@@ -88,13 +94,10 @@ class NotificationsExampleApp extends Component {
async componentDidMount() {
const initialNotification = await Notifications.getInitialNotification();
if (initialNotification) {
this.setState({notifications: [initialNotification.link, ...this.state.notifications]});
this.setState({notifications: [initialNotification, ...this.state.notifications]});
}
}
componentWillUnmount() {
}
render() {
const notifications = this.state.notifications.map((notification, idx) =>
(
......@@ -105,9 +108,9 @@ class NotificationsExampleApp extends Component {
return (
<View style={styles.container}>
<Button title={'Request permissions'} onPress={this.requestPermissions} testID={'requestPermissions'}/>
<Button title={'Send local notification'} onPress={this.sendLocalNotification} testID={'sendLocalNotification'}/>
<Button title={'Remove all delivered notifications'} onPress={this.removeAllDeliveredNotifications}/>
<Button title={'Request permissions'} onPress={this.requestPermissions} testID={'requestPermissions'} />
<Button title={'Send local notification'} onPress={this.sendLocalNotification} testID={'sendLocalNotification'} />
<Button title={'Remove all delivered notifications'} onPress={this.removeAllDeliveredNotifications} />
{notifications}
</View>
);
......
......@@ -6,7 +6,7 @@ android {
buildToolsVersion '28.0.3'
defaultConfig {
minSdkVersion 19
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
......
......@@ -6,16 +6,6 @@ public class PushNotificationProps {
protected Bundle mBundle;
public PushNotificationProps() {
mBundle = new Bundle();
}
public PushNotificationProps(String title, String body) {
mBundle = new Bundle();
mBundle.putString("title", title);
mBundle.putString("body", body);
}
public PushNotificationProps(Bundle bundle) {
mBundle = bundle;
}
......
......@@ -2,9 +2,10 @@ import { NativeCommandsSender } from './adapters/NativeCommandsSender';
import { NativeEventsReceiver } from './adapters/NativeEventsReceiver';
import { Commands } from './commands/Commands';
import { EventsRegistry } from './events/EventsRegistry';
import { Notification, NotificationCategory } from './interfaces/Notification';
import { Notification } from './interfaces/Notification';
import { UniqueIdProvider } from './adapters/UniqueIdProvider';
import { CompletionCallbackWrapper } from './adapters/CompletionCallbackWrapper';
import { NotificationCategory } from './interfaces/NotificationCategory';
export class NotificationsRoot {
private readonly nativeEventsReceiver: NativeEventsReceiver;
......
import { NativeCommandsSender } from './NativeCommandsSender';
import { NotificationCompletion, Notification } from '../interfaces/Notification';
import { Notification } from '../interfaces/Notification';
import { NotificationCompletion } from '../interfaces/NotificationCompletion';
import { Platform } from 'react-native';
export class CompletionCallbackWrapper {
constructor(
......
import { NativeModules } from 'react-native';
import { Notification, NotificationCategory, NotificationPermissions, NotificationCompletion } from '../interfaces/Notification';
import { Notification } from '../interfaces/Notification';
import { NotificationCompletion } from '../interfaces/NotificationCompletion';
import { NotificationPermissions } from '../interfaces/NotificationPermissions';
import { NotificationCategory } from '../interfaces/NotificationCategory';
interface NativeCommandsModule {
getInitialNotification(): Promise<Notification>;
......
......@@ -19,7 +19,9 @@ export class NativeEventsReceiver {
}
public registerRemoteNotificationReceived(callback: (notification: Notification) => void): EmitterSubscription {
return this.emitter.addListener('notificationReceived', callback);
return this.emitter.addListener('notificationReceived', (payload) => {
callback(new Notification(payload));
});
}
public registerPushKitNotificationReceived(callback: (event: object) => void): EmitterSubscription {
......@@ -27,7 +29,9 @@ export class NativeEventsReceiver {
}
public registerRemoteNotificationOpened(callback: (response: Notification, completion: () => void) => void): EmitterSubscription {
return this.emitter.addListener('notificationOpened', callback);
return this.emitter.addListener('notificationOpened', (payload, completion) => {
callback(new Notification(payload), completion);
});
}
public registerRemoteNotificationsRegistrationFailed(callback: (event: RegistrationError) => void): EmitterSubscription {
......
......@@ -3,8 +3,10 @@ import { mock, verify, instance, when, anyNumber } from 'ts-mockito';
import { Commands } from './Commands';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
import { Notification, NotificationCategory, NotificationPermissions } from '../interfaces/Notification';
import { Notification } from '../interfaces/Notification';
import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
import { NotificationCategory } from '../interfaces/NotificationCategory';
import { NotificationPermissions } from '../interfaces/NotificationPermissions';
describe('Commands', () => {
let uut: Commands;
......@@ -28,7 +30,7 @@ describe('Commands', () => {
});
it('returns a promise with the initial notification', async () => {
const expectedNotification: Notification = {identifier: 'id', data: {}, alert: 'alert'};
const expectedNotification: Notification = new Notification({identifier: 'id'});
when(mockedNativeCommandsSender.getInitialNotification()).thenResolve(
expectedNotification
);
......@@ -75,19 +77,19 @@ describe('Commands', () => {
describe('postLocalNotification', () => {
it('sends to native', () => {
const notification: Notification = {identifier: 'id', alert: 'alert', data: {}};
const notification: Notification = new Notification({identifier: 'id'});
uut.postLocalNotification(notification);
verify(mockedNativeCommandsSender.postLocalNotification(notification, anyNumber())).called();
});
it('generates unique identifier', () => {
const notification: Notification = {identifier: 'id', data: {}, alert: 'alert'};
const notification: Notification = new Notification({identifier: 'id'});
uut.postLocalNotification(notification);
verify(mockedNativeCommandsSender.postLocalNotification(notification, anyNumber())).called();
});
it('use passed notification id', () => {
const notification: Notification = {identifier: 'id', data: {}, alert: 'alert'};
const notification: Notification = new Notification({identifier: 'id'});
const passedId: number = 2;
uut.postLocalNotification(notification, passedId);
verify(mockedNativeCommandsSender.postLocalNotification(notification, passedId)).called();
......
import * as _ from 'lodash';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
import { Notification, NotificationCategory, NotificationPermissions } from '../interfaces/Notification';
import { Notification } from '../interfaces/Notification';
import { NotificationCategory } from '../interfaces/NotificationCategory';
import { NotificationPermissions } from '../interfaces/NotificationPermissions';
import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
export class Commands {
......
import { EventsRegistry } from './EventsRegistry';
import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver.mock';
import { NotificationCompletion, Notification } from '../interfaces/Notification';
import { Notification } from '../interfaces/Notification';
import { CompletionCallbackWrapper } from '../adapters/CompletionCallbackWrapper';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender.mock';
import { NotificationResponse } from '../interfaces/NotificationEvents';
import { Platform } from 'react-native';
import { NotificationCompletion } from '../interfaces/NotificationCompletion';
describe('EventsRegistry', () => {
let uut: EventsRegistry;
......@@ -28,7 +29,7 @@ describe('EventsRegistry', () => {
it('should wrap callback with completion block', () => {
const wrappedCallback = jest.fn();
const notification: Notification = {identifier: 'identifier', data: {}, alert: 'alert'}
const notification: Notification = new Notification({identifier: 'identifier'});
uut.registerNotificationReceived(wrappedCallback);
const call = mockNativeEventsReceiver.registerRemoteNotificationReceived.mock.calls[0][0];
......@@ -39,7 +40,7 @@ describe('EventsRegistry', () => {
});
it('should wrap callback with completion block', () => {
const expectedNotification: Notification = {identifier: 'identifier', data: {}, alert: 'alert'}
const expectedNotification: Notification = new Notification({identifier: 'identifier'});
uut.registerNotificationReceived((notification) => {
expect(notification).toEqual(expectedNotification);
......@@ -49,7 +50,7 @@ describe('EventsRegistry', () => {
});
it('should invoke finishPresentingNotification', () => {
const notification: Notification = {identifier: 'notificationId', data: {}, alert: 'alert'}
const notification: Notification = new Notification({identifier: 'notificationId'});
const response: NotificationCompletion = {alert: true}
uut.registerNotificationReceived((notification, completion) => {
......@@ -63,7 +64,7 @@ describe('EventsRegistry', () => {
it('should not invoke finishPresentingNotification on Android', () => {
Platform.OS = 'android';
const expectedNotification: Notification = {identifier: 'notificationId', data: {}, alert: 'alert'}
const expectedNotification: Notification = new Notification({identifier: 'notificationId'});
const response: NotificationCompletion = {alert: true}
uut.registerNotificationReceived((notification, completion) => {
......@@ -88,7 +89,7 @@ describe('EventsRegistry', () => {
it('should wrap callback with completion block', () => {
const wrappedCallback = jest.fn();
const notification: Notification = {identifier: 'identifier', data: {}, alert: 'alert'};
const notification: Notification = new Notification({identifier: 'identifier'});
const response: NotificationResponse = {notification, identifier: 'responseId'};
uut.registerRemoteNotificationOpened(wrappedCallback);
......@@ -100,7 +101,7 @@ describe('EventsRegistry', () => {
});
it('should wrap callback with completion block', () => {
const notification: Notification = {identifier: 'identifier', data: {}, alert: 'alert'}
const notification: Notification = new Notification({identifier: 'identifier'});
const expectedResponse: NotificationResponse = {notification, identifier: 'responseId'}
uut.registerRemoteNotificationOpened((response) => {
......@@ -111,7 +112,7 @@ describe('EventsRegistry', () => {
});
it('calling completion should invoke finishHandlingAction', () => {
const expectedNotification: Notification = {identifier: 'notificationId', data: {}, alert: 'alert'}
const expectedNotification: Notification = new Notification({identifier: 'notificationId'});
uut.registerRemoteNotificationOpened((notification, completion) => {
completion();
......@@ -125,7 +126,7 @@ describe('EventsRegistry', () => {
it('should not invoke finishHandlingAction on Android', () => {
Platform.OS = 'android';
const expectedNotification: Notification = {identifier: 'notificationId', data: {}, alert: 'alert'}
const expectedNotification: Notification = new Notification({identifier: 'notificationId'});
uut.registerRemoteNotificationOpened((notification, completion) => {
completion();
......
......@@ -7,7 +7,8 @@ import {
NotificationResponse
} from '../interfaces/NotificationEvents';
import { CompletionCallbackWrapper } from '../adapters/CompletionCallbackWrapper';
import { NotificationCompletion, Notification } from '../interfaces/Notification';
import { Notification } from '../interfaces/Notification';
import { NotificationCompletion } from '../interfaces/NotificationCompletion';
export class EventsRegistry {
constructor(
......
export interface Notification {
export class Notification {
identifier: string;
data: object;
alert: string
private _data?: any;
sound?: string;
badge?: number;
type?: string;
thread?: string;
}
export interface NotificationPermissions {
badge: boolean;
alert: boolean;
sound: boolean;
}
export interface NotificationCategory {
identifier: string
actions: [NotificationAction?];
}
constructor(payload: object) {
this._data = payload;
this.identifier = this._data.identifier;
}
export interface NotificationTextInput {
buttonTitle: string;
placeholder: string;
}
export interface NotificationAction {
identifier: string;
activationMode: 'foreground' | 'authenticationRequired' | 'destructive';
title: string;
authenticationRequired: boolean;
textInput: NotificationTextInput
}
get data(): any {
return this._data;
}
export interface NotificationActionResponse {
identifier: string;
text: string;
}
get title(): string {
return this._data.title;
}
export interface NotificationCompletion {
badge?: boolean;
alert?: boolean;
sound?: boolean;
get body(): string {
return this._data.body;
}
}
export interface NotificationActionResponse {
identifier: string;
text: string;
}
export interface NotificationCategory {
identifier: string
actions: [NotificationAction?];
}
export interface NotificationTextInput {
buttonTitle: string;
placeholder: string;
}
export interface NotificationAction {
identifier: string;
activationMode: 'foreground' | 'authenticationRequired' | 'destructive';
title: string;
authenticationRequired: boolean;
textInput: NotificationTextInput
}
\ No newline at end of file
export interface NotificationCompletion {
badge?: boolean;
alert?: boolean;
sound?: boolean;
}
import { Notification, NotificationActionResponse } from './Notification';
import { Notification } from './Notification';
import { NotificationActionResponse } from './NotificationActionResponse';
export interface Registered {
deviceToken: string;
......
export interface NotificationPermissions {
badge: boolean;
alert: boolean;
sound: boolean;
}
\ No newline at end of file
......@@ -33,7 +33,8 @@
"test-unit-ios": "node ./scripts/test-unit --ios",
"test-unit-android": "node ./scripts/test-unit --android",
"test-js": "node ./scripts/test-js",
"xcode": "open example/ios/NotificationsExampleApp.xcodeproj"
"xcode": "open example/ios/NotificationsExampleApp.xcodeproj",
"androidStudio": "open -a /Applications/Android\\ Studio.app ./example/android"
},
"nativePackage": true,
"dependencies": {
......
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