Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript react native是否具有在应用程序未使用或处于后台模式时运行生命周期的功能?_Javascript_Reactjs_React Native_Push Notification_React Native Push Notification - Fatal编程技术网

Javascript react native是否具有在应用程序未使用或处于后台模式时运行生命周期的功能?

Javascript react native是否具有在应用程序未使用或处于后台模式时运行生命周期的功能?,javascript,reactjs,react-native,push-notification,react-native-push-notification,Javascript,Reactjs,React Native,Push Notification,React Native Push Notification,react native是否具有在应用程序未使用或处于后台模式时运行生命周期的功能 我需要在react native中设置一个功能生命周期,当用户未在后台模式下使用应用程序或应用程序时,该功能将正常工作。我想检查数据库以获取通知组件DidMount是否正常工作,但用户必须打开应用程序并再次关闭它 我需要一个功能或生命周期始终有效 这是我的通知代码: import React, { Component } from 'react'; import { View, NetInfo, Image, A

react native是否具有在应用程序未使用或处于后台模式时运行生命周期的功能

我需要在react native中设置一个功能生命周期,当用户未在后台模式下使用应用程序或应用程序时,该功能将正常工作。我想检查数据库以获取通知组件DidMount是否正常工作,但用户必须打开应用程序并再次关闭它

我需要一个功能或生命周期始终有效

这是我的通知代码:

import React, { Component } from 'react';
import { View, NetInfo, Image, AppState, DeviceEventEmitter } from 'react-native';
import { Container, Text } from 'native-base';
import { RootNavigator } from './src/root';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import resducers from './src/reducers/index';
import PushController from'./PushController';
import PushNotification from 'react-native-push-notification';
import PushNotificationAndroid from 'react-native-push-notification';

const store = createStore(resducers);

export default class App extends Component<Props> {
  constructor(Props){
    super(Props);
    this.state={
      connection:null,
    }
    this.handleAppStateChange = this.handleAppStateChange.bind(this);
    this.sendNotification = this.sendNotification.bind(this);
  }

  componentDidMount(){
    AppState.addEventListener('change',this.handleAppStateChange);
  }

  componentWillMount(){
    NetInfo.isConnected.addEventListener("connectionChange",this.handleConnectionChange);
    NetInfo.isConnected.fetch().done((isConnected)=>this.setState({connection:isConnected}));

    PushNotificationAndroid.registerNotificationActions(['Accept','Reject','Yes','No']);
    DeviceEventEmitter.addListener('notificationActionReceived', function(e){
      console.log ('notificationActionReceived event received: ' + e);
      const info = JSON.parse(e.dataJSON);
      if (info.action == 'Yes') {
        alert('Accept');
      } else if (info.action == 'No') {
        alert('Reject')
      }
      // Add all the required actions handlers
    });
  }

  componentWillUnMount(){
    NetInfo.isConnected.removeEventListener("connectionChange",this.handleConnectionChange);
    AppState.removeEventListener('change',this.handleAppStateChange);
  }

  handleConnectionChange=(isConnected)=>{
    this.setState({connection:isConnected});
  }

  handleAppStateChange(appState){
    if(appState==='background'){
      PushNotification.localNotificationSchedule({
        message:'Scheduled notification delay message',
        date:new Date(Date.now()+(2000))
      })
    }
  }
  sendNotification(){
    PushNotification.localNotification({
      message:'You Pushed the notification button',
      title:'My Notification Title',
      ongoing:true,
      vibrate:true,
      playSound:true,
      actions:'["Yes","No"]',
      color:'red'
    })
  }

  handeView(){
    if(this.state.connection!==null && this.state.connection){
      return <RootNavigator />
    }else {
      return <View style={{flex:1, alignItems:"center", justifyContent:"center"}}>
         <Image source={require("./images/connection.gif")} style={{height: 150, width: 150, resizeMode : "stretch"}}/>
         <Text style={{ fontFamily:"IRANSans", fontSize:18, textAlign:"center", color:"#b0b5bb" }}>لطفا اتصال اینترنت خود را بررسی کنید ...</Text>
      </View>
    }
  }

  render() {
    return (
      <Provider store={store}>
         <Container>
             {this.handeView()}
             {this.sendNotification()}
         </Container>
      </Provider>
    );
  }
}
import React,{Component}来自'React';
从“react native”导入{View、NetInfo、Image、AppState、DeviceEventEmitter};
从“本机基”导入{Container,Text};
从“./src/root”导入{RootNavigator};
从'react redux'导入{Provider};
从“redux”导入{createStore};
从“/src/reducers/index”导入resducers;
从“/PushController”导入PushController;
从“反应本机推送通知”导入推送通知;
从“react native push notification”导入PushNotificationAndroid;
const store=createStore(resducers);
导出默认类应用程序扩展组件{
建造师(道具){
超级(道具);
这个州={
连接:空,
}
this.handlappstatechange=this.handlappstatechange.bind(this);
this.sendNotification=this.sendNotification.bind(this);
}
componentDidMount(){
AppState.addEventListener('change',this.handleAppStateChange);
}
组件willmount(){
NetInfo.isConnected.addEventListener(“connectionChange”,this.handleConnectionChange);
NetInfo.isConnected.fetch().done((isConnected)=>this.setState({connection:isConnected}));
PushNotificationAndroid.registerNotificationActions(['Accept','Reject','Yes','No']);
DeviceEventEmitter.addListener('notificationActionReceived',函数(e){
console.log('notificationActionReceived event received:'+e);
const info=JSON.parse(e.dataJSON);
如果(info.action=='Yes'){
警报(“接受”);
}否则如果(info.action==“否”){
警报(“拒绝”)
}
//添加所有必需的操作处理程序
});
}
组件将卸载(){
NetInfo.isConnected.removeEventListener(“connectionChange”,this.handleConnectionChange);
AppState.removeEventListener('change',this.handleAppStateChange);
}
handleConnectionChange=(未连接)=>{
this.setState({connection:isConnected});
}
handleAppStateChange(appState){
如果(appState==='background'){
PushNotification.localNotificationSchedule({
消息:'Scheduled notification delay message',
日期:新日期(date.now()+(2000))
})
}
}
发送通知(){
PushNotification.localNotification({
消息:'您按下了通知按钮',
标题:'我的通知标题',
正在进行的:是的,
振动:是的,
playSound:没错,
措施:[“是”、“否”],
颜色:'红色'
})
}
handeView(){
if(this.state.connection!==null&&this.state.connection){
返回
}否则{
返回
لطفا اتصال اینترنت خود را بررسی کنید ...
}
}
render(){
返回(
{this.handeView()}
{this.sendNotification()}
);
}
}

正如@dentemm提到的,这需要在本机代码中运行,以便您可以尝试使用此模块


注意:对于通知,我们通常使用外部服务向用户推送通知,例如Firebase,Amazone SNS或Google Cloud消息即使你的应用程序完全关闭,通知也会到达用户,因为操作系统将处理通知,并在用户单击时打开你的应用程序并为你运行一个功能。有关更多详细信息,你可以查看本教程

JavaScript代码不能在后台运行,因此,如果您想要后台处理,您需要为iOS和Android创建一个本机模块