【问题标题】:How to create local push notification using react-native-push-notification如何使用 react-native-push-notification 创建本地推送通知
【发布时间】:2017-08-19 21:08:30
【问题描述】:

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

【问题讨论】:

  • 您可以使用react-native-fcmreact-native-push-notification 进行本地推送通知以及远程通知。
  • 谢谢@Hariks,但我不明白如何使用代码。我应该在哪里调用函数。
  • 在“使用”中,他们给出了一些我不理解的代码,在哪里编写该代码?
  • 反应原生部分还是你添加包的原生部分?
  • 反应原生部分

标签: android react-native push-notification


【解决方案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】:
    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);
    

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

    【讨论】:

      【解决方案3】:

      你也可以试试

      react-native-notifications

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

      1.远程(推送)通知

      2.本地通知

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

      4.PushKit API(用于 VoIP 和其他后台消息)

      5.交互式通知(允许您在应用程序之外向用户提供附加功能,例如操作按钮)

      代码 sn-ps -->

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

      这对我来说非常有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2021-07-04
        • 1970-01-01
        • 2019-10-03
        • 2019-08-25
        相关资源
        最近更新 更多