Reactjs 响应本地推送通知(如Facebook),而不是使用FCM

Reactjs 响应本地推送通知(如Facebook),而不是使用FCM,reactjs,react-native,websocket,push-notification,reactive-programming,Reactjs,React Native,Websocket,Push Notification,Reactive Programming,我对React Native开发还不熟悉,我正在使用WebSocket开发聊天应用程序 我可以发送和接收消息,但当消息来自频道时,我需要显示本地通知 我正在接收消息,我可以在应用程序打开时显示通知,但如果它在后台运行,我不会接收通知 仅供参考:我没有关闭应用程序,我打开了另一个应用程序,如WhatsApp或任何应用程序,如果我收到消息,通知也不会显示 对于本地推送通知,am使用: “反应本机推送通知”:“3.1.1” “本机反应”:“0.59.8” 我不想使用任何第三方服务,如FCM 下面

我对React Native开发还不熟悉,我正在使用WebSocket开发聊天应用程序

我可以发送和接收消息,但当消息来自频道时,我需要显示本地通知

我正在接收消息,我可以在应用程序打开时显示通知,但如果它在后台运行,我不会接收通知

仅供参考:我没有关闭应用程序,我打开了另一个应用程序,如WhatsApp或任何应用程序,如果我收到消息,通知也不会显示

对于本地推送通知,am使用:

  • “反应本机推送通知”:“3.1.1”

  • “本机反应”:“0.59.8”

我不想使用任何第三方服务,如FCM

下面是获取和显示通知的代码

import React, {Component} from 'react';
import {Socket} from 'phoenix-socket';
import AsyncStorage from "@react-native-community/async-storage";
import PushNotification from 'react-native-push-notification';

class SocketConnection extends React.Component {

  constructor(props) {
    console.log('Socket constructor');
    super(props);
    this.state = {
        user_id: '',
        company_id: '',
        msg: {}
    };
    PushNotification.configure({
        onNotification: function (notification) {
            console.log('NOTIFICATION: ', notification);
        },
        popInitialNotification: true,
        requestPermissions: true,
    });

}

componentDidMount() {
    this.getMessage();
};

getMessage = async () => {
    var thatclass = this;
    let user = await AsyncStorage.getItem('currentUser');
    user = JSON.parse(user);
    this.setState({user_id: user.user.id});
    this.setState({company_id: user.user.company_id});
    console.log('Socket constructor');
    this.socket = new Socket('ws://localhost:4000/socket/websocket');
    this.socket.connect();
    const that = this;
    this.socket.onError(function (er) {
        console.log('Error');
        console.log(er);
        that.socket.close();
    });
    this.socket.onOpen(function (res) {
        console.log('Open');
        console.log(res)

    });

    this.channel = this.socket.channel("channel:" + user.user.company_id, {});
    this.channel.join()
        .receive("ok", resp => {
            console.log(resp)
        })
        .receive("error", resp => {
            console.log(resp)
        });
    if (this.channel) {
        this.channel.on('users:' + user.user.id, function (payload) {
            console.log(payload);
            this.setState = ({
                msg: payload,
            });
            thatclass.sendNotification(payload);
        });
    }
}

sendNotification(payload) {
    console.log(payload);

    PushNotification.localNotification({
        foreground: true, // BOOLEAN: If the notification was received in foreground or not
        userInteraction: false,
        ticker: "My Notification Ticker", // (optional)
        bigText: payload.message, // (optional) default: "message" prop
        color: "red", // (optional) default: system default
        priority: "high", // (optional) set notification priority, default: high
        visibility: "private", // (optional) set notification visibility, default: private
        /* iOS and Android properties */
        title: payload.subject, // (optional)
        message: "My Notification Message", // (required)
        playSound: true, // (optional) default: true
        actions: '["archive", "reply"]',
    });
};

openIndex(id) {
    console.log(id);
}

 render() {
     return (null);
   }
 }

  export default SocketConnection;