How to create local push notification using react-

2019-07-19 03:18发布

问题:

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.