Ver código fonte

Navigation interface for showing in-app notifications in iOS

Artal Druk 8 anos atrás
pai
commit
a89c088076

+ 9
- 0
example/src/screens/FirstTabScreen.js Ver arquivo

@@ -67,6 +67,10 @@ export default class FirstTabScreen extends Component {
67 67
           <Text style={styles.button}>Show LightBox</Text>
68 68
         </TouchableOpacity>
69 69
 
70
+        <TouchableOpacity onPress={ this.onInAppNotificationPress.bind(this) }>
71
+          <Text style={styles.button}>Show In-App Notification</Text>
72
+        </TouchableOpacity>
73
+
70 74
       </View>
71 75
     );
72 76
   }
@@ -96,6 +100,11 @@ export default class FirstTabScreen extends Component {
96 100
       }
97 101
     });
98 102
   }
103
+  onInAppNotificationPress() {
104
+    this.props.navigator.showInAppNotification({
105
+      screen: "example.NotificationScreen"
106
+    });
107
+  }
99 108
 }
100 109
 
101 110
 const styles = StyleSheet.create({

+ 52
- 0
example/src/screens/NotificationScreen.js Ver arquivo

@@ -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
+});

+ 1
- 0
example/src/screens/index.android.js Ver arquivo

@@ -7,6 +7,7 @@ import PushedScreen from './PushedScreen';
7 7
 import StyledScreen from './StyledScreen';
8 8
 import ModalScreen from './ModalScreen';
9 9
 import LightBoxScreen from './LightBoxScreen';
10
+import NotificationScreen from './NotificationScreen'
10 11
 import SideMenu from './SideMenu';
11 12
 
12 13
 // register all screens of the app (including internal ones)

+ 2
- 0
example/src/screens/index.ios.js Ver arquivo

@@ -7,6 +7,7 @@ import PushedScreen from './PushedScreen';
7 7
 import StyledScreen from './StyledScreen';
8 8
 import ModalScreen from './ModalScreen';
9 9
 import LightBoxScreen from './LightBoxScreen';
10
+import NotificationScreen from './NotificationScreen'
10 11
 import SideMenu from './SideMenu';
11 12
 
12 13
 // register all screens of the app (including internal ones)
@@ -18,5 +19,6 @@ export function registerScreens() {
18 19
   Navigation.registerComponent('example.StyledScreen', () => StyledScreen);
19 20
   Navigation.registerComponent('example.ModalScreen', () => ModalScreen);
20 21
   Navigation.registerComponent('example.LightBoxScreen', () => LightBoxScreen);
22
+  Navigation.registerComponent('example.NotificationScreen', () => NotificationScreen);
21 23
   Navigation.registerComponent('example.SideMenu', () => SideMenu);
22 24
 }

+ 1
- 1
package.json Ver arquivo

@@ -7,7 +7,7 @@
7 7
     "type": "git",
8 8
     "url": "https://github.com/wix/react-native-navigation.git"
9 9
   },
10
-  "version": "1.0.17",
10
+  "version": "1.0.18",
11 11
   "description": "React Native Navigation - truly native navigation for iOS and Android",
12 12
   "nativePackage": true,
13 13
   "bugs": {

+ 10
- 0
src/Navigation.js Ver arquivo

@@ -83,6 +83,14 @@ function dismissLightBox(params = {}) {
83 83
   return platformSpecific.dismissLightBox(params);
84 84
 }
85 85
 
86
+function showInAppNotification(params = {}) {
87
+  return platformSpecific.showInAppNotification(params);
88
+}
89
+
90
+function dismissInAppNotification(params = {}) {
91
+  return platformSpecific.dismissInAppNotification(params);
92
+}
93
+
86 94
 export default {
87 95
   registerScreen,
88 96
   getRegisteredScreen,
@@ -92,6 +100,8 @@ export default {
92 100
   dismissAllModals,
93 101
   showLightBox,
94 102
   dismissLightBox,
103
+  showInAppNotification,
104
+  dismissInAppNotification,
95 105
   startTabBasedApp: platformSpecific.startTabBasedApp,
96 106
   startSingleScreenApp: platformSpecific.startSingleScreenApp
97 107
 }

+ 8
- 0
src/Screen.js Ver arquivo

@@ -53,6 +53,14 @@ class Navigator {
53 53
     return Navigation.dismissLightBox(params);
54 54
   }
55 55
 
56
+  showInAppNotification(params = {}) {
57
+    return Navigation.showInAppNotification(params);
58
+  }
59
+
60
+  dismissInAppNotification(params = {}) {
61
+    return Navigation.dismissInAppNotification(params);
62
+  }
63
+
56 64
   setButtons(params = {}) {
57 65
     return platformSpecific.navigatorSetButtons(this, this.navigatorEventID, params);
58 66
   }

+ 38
- 1
src/platformSpecific.ios.js Ver arquivo

@@ -1,6 +1,6 @@
1 1
 import utils from './utils';
2 2
 import Navigation from './Navigation';
3
-import Controllers, { Modal } from 'react-native-controllers';
3
+import Controllers, { Modal, Notification } from 'react-native-controllers';
4 4
 const React = Controllers.hijackReact();
5 5
 const {
6 6
   ControllerRegistry,
@@ -404,6 +404,41 @@ function dismissLightBox(params) {
404 404
   Modal.dismissLightBox();
405 405
 }
406 406
 
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
+
407 442
 export default {
408 443
   startTabBasedApp,
409 444
   startSingleScreenApp,
@@ -416,6 +451,8 @@ export default {
416 451
   dismissAllModals,
417 452
   showLightBox,
418 453
   dismissLightBox,
454
+  showInAppNotification,
455
+  dismissInAppNotification,
419 456
   navigatorSetButtons,
420 457
   navigatorSetTitle,
421 458
   navigatorSetTitleImage,