首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在按下按钮时显示react-native-push-notification popup?

如何在按下按钮时显示react-native-push-notification popup?
EN

Stack Overflow用户
提问于 2020-11-05 17:35:27
回答 1查看 678关注 0票数 0

我是一个新的react native和学习react-native-notification-popup的人。我需要一点帮助。在我的应用程序中,当屏幕打开时,componentDidMountFunction会显示通知。我想创建一个按钮,当按下按钮时,通知应该会显示出来。代码如下:

代码语言:javascript
复制
const renderCustomPopup = ({ appIconSource, appTitle, timeText, title, body }) => (
  <View>
    <Text>{title}</Text>
    <Text>{body}</Text>
    <Button title='My button' onPress={() => console.log('Popup button onPress!')} />
  </View>
);
export default class MyApp extends Component {
  componentDidMount() {
    this.popup.show({
      onPress: function() {console.log('Pressed')},
      appIconSource: require('./assets/icon.png'),
      appTitle: 'Some App',
      timeText: 'Now',
      title: 'Hello World',
      body: 'This is a sample message.\nTesting emoji ?',
      slideOutTime: 5000
    });
  }

  render() {
    return (
      <View style={styles.container}>
        <NotificationPopup
          ref={ref => this.popup = ref}
          renderPopupContent={renderCustomPopup}
          shouldChildHandleResponderStart={true}
          shouldChildHandleResponderMove={true} />
      </View>
    );
  }
}

我想要的是:

代码语言:javascript
复制
export default class AddProfile extends Component {
    //...
    render() {    
    return (
      <View style={styles.container}>
        <TouchableOpacity 
          style={styles.btn}
          onPress={() => 
            //how to show same notification on onPress
            }
          >
          <Text style={styles.loginText}>Show Notification</Text>
        </TouchableOpacity>
      </View>

    );
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-05 18:15:03

只需重命名您的componentDidMount函数并将其命名为onPress。喜欢

代码语言:javascript
复制
showPopUp= () => {
    this.popup.show({
      onPress: function() {console.log('Pressed')},
      appIconSource: require('./assets/icon.png'),
      appTitle: 'Some App',
      timeText: 'Now',
      title: 'Hello World',
      body: 'This is a sample message.\nTesting emoji ?',
      slideOutTime: 5000
    });
  }

并这样调用它: onPress={() => this.showPopUp()}将您的类修改为

代码语言:javascript
复制
export default class AddProfile extends Component {
    //...
    render() {    
    return (
      <View style={styles.container}>
    <NotificationPopup
          ref={ref => this.popup = ref}
          renderPopupContent={renderCustomPopup}
          shouldChildHandleResponderStart={true}
          shouldChildHandleResponderMove={true} />
        <TouchableOpacity 
          style={styles.btn}
          onPress={() => this.showPopUp()
            }
          >
          <Text style={styles.loginText}>Show Notification</Text>
        </TouchableOpacity>
      </View>

    );
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64694535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档