如何使用react-native-push-notification创建本地推送通知

时间:2017-03-27 08:42:07

标签: android react-native push-notification

我正在使用react-native开发一个Android应用程序,我想使用本地推送通知,就像我点击按钮时,推送通知应该创建。我怎样才能做到这一点? 有人请给我一些建议。

3 个答案:

答案 0 :(得分:1)

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Button,
 TouchableHighlight
 } from 'react-native';

import PushNotification from 'react-native-push-notification';

export default class pn extends Component {
  scheduleNotfication() { 
PushNotification.localNotificationSchedule({ 
message: "My Notification Message", // message 
date: new Date(Date.now() + (60 * 1000)) // your required time 
}); 
}
  render() {
    return (
      <View>


<TouchableHighlight onPress ={this.scheduleNotfication.bind(this) } >
<Text>show</Text> 
</TouchableHighlight>

      </View>
    );
  }
}


AppRegistry.registerComponent('pn', () => pn);

这是完美的,并在一定时间内获得本地推送通知。

答案 1 :(得分:1)

您可以使用 react-native-push-notification

尝试此操作
import PushNotification from 'react-native-push-notification';

scheduleNotfication() { 
 PushNotification.localNotificationSchedule({ 
 message: "My Notification Message", // message 
 date: new Date(Date.now() + (60 * 1000)) // your required time 
 }); 
}  

按钮

<Button title="title of button" onPress ={this.scheduleNotfication() } > 
<Text>show</Text> 
</Button>

答案 2 :(得分:0)

您也可以尝试

react-native-notifications

它可以帮助您进行本地和远程推送通知。

1.Remote(推送)通知

2.本地通知

3.Background /托管通知(可以从服务器清除的通知,如Facebook Messenger和Whatsapp网站)

4.PushKit API(用于VoIP和其他背景信息)

5.Interactive通知(允许您为应用程序以外的用户提供其他功能,例如操作按钮)

代码段 - &gt;

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Button,
 TouchableHighlight
 } from 'react-native';


import {NotificationsAndroid} from 'react-native-notifications';

export default class pushLocalNotification extends Component {
  get_Local_Notfication() { 
    NotificationsAndroid.localNotification({
      title: "Local notification",
      body: "This notification was generated by the app!",
      extra: "data"
    });
  }
  render() {
    return (
      <View>    
           <TouchableHighlight onPress =
                    {this.get_Local_Notfication.bind(this) } >
              <Text>show</Text> 
           </TouchableHighlight>
      </View>
    );
  }
}


AppRegistry.registerComponent('pushLocalNotification', () => 
    pushLocalNotification);

这对我来说非常合适。