Skip to content

Commit a89c088

Browse files
committed
Navigation interface for showing in-app notifications in iOS
1 parent da0c997 commit a89c088

File tree

8 files changed

+121
-2
lines changed

8 files changed

+121
-2
lines changed

example/src/screens/FirstTabScreen.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export default class FirstTabScreen extends Component {
6767
<Text style={styles.button}>Show LightBox</Text>
6868
</TouchableOpacity>
6969

70+
<TouchableOpacity onPress={ this.onInAppNotificationPress.bind(this) }>
71+
<Text style={styles.button}>Show In-App Notification</Text>
72+
</TouchableOpacity>
73+
7074
</View>
7175
);
7276
}
@@ -96,6 +100,11 @@ export default class FirstTabScreen extends Component {
96100
}
97101
});
98102
}
103+
onInAppNotificationPress() {
104+
this.props.navigator.showInAppNotification({
105+
screen: "example.NotificationScreen"
106+
});
107+
}
99108
}
100109

101110
const styles = StyleSheet.create({
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React, {Component} from 'react';
2+
import {
3+
StyleSheet,
4+
Text,
5+
View,
6+
TouchableOpacity,
7+
Dimensions
8+
} from 'react-native';
9+
10+
export default class NotificationScreen extends Component {
11+
constructor(props) {
12+
super(props);
13+
}
14+
render() {
15+
return (
16+
<View style={styles.container}>
17+
<Text style={styles.welcome}>
18+
This is a Notification!
19+
</Text>
20+
<TouchableOpacity onPress={ this.onDismissPress.bind(this) }>
21+
<Text style={styles.button}>Dismiss</Text>
22+
</TouchableOpacity>
23+
</View>
24+
);
25+
}
26+
onDismissPress() {
27+
this.props.navigator.dismissInAppNotification();
28+
}
29+
}
30+
31+
var styles = StyleSheet.create({
32+
container: {
33+
flex: 1,
34+
width: Dimensions.get('window').width,
35+
justifyContent: 'center',
36+
alignItems: 'center',
37+
backgroundColor: '#d6e7ad'
38+
},
39+
welcome: {
40+
fontSize: 20,
41+
textAlign: 'center',
42+
margin: 10,
43+
paddingTop: 20
44+
},
45+
button: {
46+
textAlign: 'center',
47+
fontSize: 18,
48+
marginBottom: 10,
49+
marginTop:10,
50+
color: '#4692ad'
51+
}
52+
});

example/src/screens/index.android.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import PushedScreen from './PushedScreen';
77
import StyledScreen from './StyledScreen';
88
import ModalScreen from './ModalScreen';
99
import LightBoxScreen from './LightBoxScreen';
10+
import NotificationScreen from './NotificationScreen'
1011
import SideMenu from './SideMenu';
1112

1213
// register all screens of the app (including internal ones)

example/src/screens/index.ios.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import PushedScreen from './PushedScreen';
77
import StyledScreen from './StyledScreen';
88
import ModalScreen from './ModalScreen';
99
import LightBoxScreen from './LightBoxScreen';
10+
import NotificationScreen from './NotificationScreen'
1011
import SideMenu from './SideMenu';
1112

1213
// register all screens of the app (including internal ones)
@@ -18,5 +19,6 @@ export function registerScreens() {
1819
Navigation.registerComponent('example.StyledScreen', () => StyledScreen);
1920
Navigation.registerComponent('example.ModalScreen', () => ModalScreen);
2021
Navigation.registerComponent('example.LightBoxScreen', () => LightBoxScreen);
22+
Navigation.registerComponent('example.NotificationScreen', () => NotificationScreen);
2123
Navigation.registerComponent('example.SideMenu', () => SideMenu);
2224
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "git",
88
"url": "https://github.com/wix/react-native-navigation.git"
99
},
10-
"version": "1.0.17",
10+
"version": "1.0.18",
1111
"description": "React Native Navigation - truly native navigation for iOS and Android",
1212
"nativePackage": true,
1313
"bugs": {

src/Navigation.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ function dismissLightBox(params = {}) {
8383
return platformSpecific.dismissLightBox(params);
8484
}
8585

86+
function showInAppNotification(params = {}) {
87+
return platformSpecific.showInAppNotification(params);
88+
}
89+
90+
function dismissInAppNotification(params = {}) {
91+
return platformSpecific.dismissInAppNotification(params);
92+
}
93+
8694
export default {
8795
registerScreen,
8896
getRegisteredScreen,
@@ -92,6 +100,8 @@ export default {
92100
dismissAllModals,
93101
showLightBox,
94102
dismissLightBox,
103+
showInAppNotification,
104+
dismissInAppNotification,
95105
startTabBasedApp: platformSpecific.startTabBasedApp,
96106
startSingleScreenApp: platformSpecific.startSingleScreenApp
97107
}

src/Screen.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ class Navigator {
5353
return Navigation.dismissLightBox(params);
5454
}
5555

56+
showInAppNotification(params = {}) {
57+
return Navigation.showInAppNotification(params);
58+
}
59+
60+
dismissInAppNotification(params = {}) {
61+
return Navigation.dismissInAppNotification(params);
62+
}
63+
5664
setButtons(params = {}) {
5765
return platformSpecific.navigatorSetButtons(this, this.navigatorEventID, params);
5866
}

src/platformSpecific.ios.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import utils from './utils';
22
import Navigation from './Navigation';
3-
import Controllers, { Modal } from 'react-native-controllers';
3+
import Controllers, { Modal, Notification } from 'react-native-controllers';
44
const React = Controllers.hijackReact();
55
const {
66
ControllerRegistry,
@@ -404,6 +404,41 @@ function dismissLightBox(params) {
404404
Modal.dismissLightBox();
405405
}
406406

407+
function showInAppNotification(params) {
408+
if (!params.screen) {
409+
console.error('showInAppNotification(params): params.screen is required');
410+
return;
411+
}
412+
413+
const controllerID = utils.getRandomId();
414+
const navigatorID = controllerID + '_nav';
415+
const screenInstanceID = utils.getRandomId();
416+
const {
417+
navigatorStyle,
418+
navigatorButtons,
419+
navigatorEventID
420+
} = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
421+
const passProps = Object.assign({}, params.passProps);
422+
passProps.navigatorID = navigatorID;
423+
passProps.screenInstanceID = screenInstanceID;
424+
passProps.navigatorEventID = navigatorEventID;
425+
426+
Notification.show({
427+
component: params.screen,
428+
passProps: passProps,
429+
style: params.style,
430+
animation: params.animation || Notification.AnimationPresets.default,
431+
position: params.position,
432+
shadowRadius: params.shadowRadius,
433+
dismissWithSwipe: params.dismissWithSwipe || true,
434+
autoDismissTimerSec: params.autoDismissTimerSec || 5
435+
});
436+
}
437+
438+
function dismissInAppNotification(params) {
439+
Notification.dismiss(params);
440+
}
441+
407442
export default {
408443
startTabBasedApp,
409444
startSingleScreenApp,
@@ -416,6 +451,8 @@ export default {
416451
dismissAllModals,
417452
showLightBox,
418453
dismissLightBox,
454+
showInAppNotification,
455+
dismissInAppNotification,
419456
navigatorSetButtons,
420457
navigatorSetTitle,
421458
navigatorSetTitleImage,

0 commit comments

Comments
 (0)