react-native推送通知在后台不起作用

时间:2018-03-21 11:18:45

标签: react-native push-notification react-native-fcm

我正在使用react-native-fcm来接收推送通知。当应用程序通知屏幕仅处于前台状态时(在其他屏幕上无法正常工作),它正常工作。但我希望在应用程序被杀或接收时收到通知背景或我在我的项目的任何其他屏幕上。这是我的代码:

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View
} from 'react-native';
import FCM,{FCMEvent,RemoteNotificationResult,WillPresentNotificationResult,NotificationType} from "react-native-fcm";

let data=[];
FCM.on(FCMEvent.Notification, async(notify)=>{
  if(notify.local_notification){
    //this is a local notification
    console.log('===data  =====on=='+JSON.stringify(notify))
  }
  if(notify.opened_from_tray){
    console.log("====tray=======")
  }
  if(Platform.OS==='android'){
    switch(notify.NotificationType){
      case NotificationType.Remote:
      notify.finish(
        RemoteNotificationResult.NewData  
      )                   
      break;
      case NotificationType.NotificationResponse:
      notify.finish();
      break;
      case NotificationType.WillPresent:
      notify.finish(WillPresentNotificationResult.All)
      break;
    }           
  }
});
FCM.on(FCMEvent.RefreshToken,(token)=>{
  console.log(token);
})

export default class HomePage extends Component {

  constructor(props){
    super(props);
    this.state={
      totalTarget: null,
      targetAchieved:null,
      message:null,
      notificationsType:null,
      extraMessage:null,
    }
  }

  componentDidMount () {
    const { navigate } = this.props.navigation;
    FCM.requestPermissions().then(()=>console.log('granted')).catch(()=>console.log('notification permission'))

    FCM.getFCMToken().then(token=>{
      console.log(token)
    });

    this.notificationListener = FCM.on(FCMEvent.Notification, async(notify)=>{
      console.log("===notification---listener===="+JSON.stringify(notify));
      this.setState({
        totalTarget:notify.totalTarget,
        targetAchieved:notify.targetAchieved,
        message:notify.message,
        notificationsType:notify.notificationsType,
        extraMessage:notify.extraMessage
      })
      navigate("TargetScreenPage",{message:this.state.message,extraMessage:this.state.extraMessage})
    })

    FCM.getInitialNotification().then(notify=>{
      console.log("notification-----"+ JSON.stringify(notify))

    })
  }

  componentWillUnmount(){
    this.notificationListener.remove();           
  }

  render() {
    return (
      <View style={styles.container}>
         <Text style={styles.instructions}>
          {this.state.totalTarget}
        </Text>
        <Text style={styles.instructions}>
          {this.state.targetAchieved}
        </Text>

         <Text style={styles.instructions}>
          {this.state.message}
        </Text>
        <Text style={styles.instructions}>
          {this.state.notificationsType}
        </Text>
     <Text style={styles.instructions}>
          {this.state.extraMessage}
        </Text>

      </View>
    );
  }
}

0 个答案:

没有答案