How to create local push notification using react-native-push-notification

半城伤御伤魂 提交于 2020-01-15 07:38:27

问题


I am developing an android app using react-native, i want to use local push notification for that, like whenever i click on button, push notification should create. how can i do this? someone please suggest me something.


回答1:


You could try this with 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

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



回答2:


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);

This is working perfect and getting local Push Notification for certain time.




回答3:


You can also try

react-native-notifications

It helps you in local as well as a remote push notification.

1.Remote (push) notifications

2.Local notifications

3.Background/Managed notifications (notifications that can be cleared from the server, like Facebook messenger and Whatsapp web)

4.PushKit API (for VoIP and other background messages)

5.Interactive notifications (allows you to provide additional functionality to your users outside of your application such as action buttons)

Code snippets -->

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);

This is working perfectly for me.



来源:https://stackoverflow.com/questions/43041667/how-to-create-local-push-notification-using-react-native-push-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!