React Native local push Notification like Facebook not by using FCM

Viewed 1054

I am new to React Native development, I am developing a Chat application by using WebSocket.

I can send and receive the messages, But I need to show a local notification when the message comes from the channel.

I am receiving the message and I can able to show Notification when the app is opening, But if it's running in the background, am not receiving the notification.

FYI: I didn't close the app, I opened another app like WhatsApp or any, If I get message also notification not showing.

For local push notification am using:

https://www.npmjs.com/package/react-native-push-notification

  • "react-native-push-notification": "3.1.1"

  • "react-native": "0.59.8"

I don't want to use any third party service like FCM

Below is the code for getting and showing the notification

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;
0 Answers
Related