mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 20:29:17 +00:00
fix: notifications not working sometimes
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
"react-toastify": "^6.0.9",
|
"react-toastify": "^6.0.9",
|
||||||
"recharts": "^1.8.5",
|
"recharts": "^1.8.5",
|
||||||
"socket.io-client": "^2.3.1",
|
"socket.io-client": "^2.3.1",
|
||||||
|
"use-sound": "^1.0.2",
|
||||||
"yup": "^0.29.3"
|
"yup": "^0.29.3"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import React, { useState, useRef, useEffect } from "react";
|
|||||||
import { useHistory } from "react-router-dom";
|
import { useHistory } from "react-router-dom";
|
||||||
import { format } from "date-fns";
|
import { format } from "date-fns";
|
||||||
import openSocket from "socket.io-client";
|
import openSocket from "socket.io-client";
|
||||||
|
import useSound from "use-sound";
|
||||||
|
|
||||||
import Popover from "@material-ui/core/Popover";
|
import Popover from "@material-ui/core/Popover";
|
||||||
import IconButton from "@material-ui/core/IconButton";
|
import IconButton from "@material-ui/core/IconButton";
|
||||||
@@ -16,6 +17,7 @@ import ChatIcon from "@material-ui/icons/Chat";
|
|||||||
import TicketListItem from "../TicketListItem";
|
import TicketListItem from "../TicketListItem";
|
||||||
import { i18n } from "../../translate/i18n";
|
import { i18n } from "../../translate/i18n";
|
||||||
import useTickets from "../../hooks/useTickets";
|
import useTickets from "../../hooks/useTickets";
|
||||||
|
import alertSound from "../../assets/sound.mp3";
|
||||||
|
|
||||||
const useStyles = makeStyles(theme => ({
|
const useStyles = makeStyles(theme => ({
|
||||||
tabContainer: {
|
tabContainer: {
|
||||||
@@ -42,22 +44,27 @@ const NotificationsPopOver = () => {
|
|||||||
|
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const userId = +localStorage.getItem("userId");
|
const userId = +localStorage.getItem("userId");
|
||||||
const soundAlert = useRef(new Audio(require("../../assets/sound.mp3")));
|
|
||||||
const ticketIdUrl = +history.location.pathname.split("/")[2];
|
const ticketIdUrl = +history.location.pathname.split("/")[2];
|
||||||
const ticketIdRef = useRef(ticketIdUrl);
|
const ticketIdRef = useRef(ticketIdUrl);
|
||||||
const anchorEl = useRef();
|
const anchorEl = useRef();
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [notifications, setNotifications] = useState([]);
|
const [notifications, setNotifications] = useState([]);
|
||||||
|
|
||||||
|
const [, setDesktopNotifications] = useState([]);
|
||||||
|
|
||||||
const { tickets } = useTickets({ withUnreadMessages: "true" });
|
const { tickets } = useTickets({ withUnreadMessages: "true" });
|
||||||
|
const [play] = useSound(alertSound);
|
||||||
|
const soundAlertRef = useRef();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
soundAlertRef.current = play;
|
||||||
|
|
||||||
if (!("Notification" in window)) {
|
if (!("Notification" in window)) {
|
||||||
console.log("This browser doesn't support notifications");
|
console.log("This browser doesn't support notifications");
|
||||||
} else {
|
} else {
|
||||||
Notification.requestPermission();
|
Notification.requestPermission();
|
||||||
}
|
}
|
||||||
}, []);
|
}, [play]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setNotifications(tickets);
|
setNotifications(tickets);
|
||||||
@@ -82,6 +89,18 @@ const NotificationsPopOver = () => {
|
|||||||
}
|
}
|
||||||
return prevState;
|
return prevState;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setDesktopNotifications(prevState => {
|
||||||
|
const notfiticationIndex = prevState.findIndex(
|
||||||
|
n => n.tag === String(data.ticketId)
|
||||||
|
);
|
||||||
|
if (notfiticationIndex !== -1) {
|
||||||
|
prevState[notfiticationIndex].close();
|
||||||
|
prevState.splice(notfiticationIndex, 1);
|
||||||
|
return [...prevState];
|
||||||
|
}
|
||||||
|
return prevState;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -108,31 +127,7 @@ const NotificationsPopOver = () => {
|
|||||||
|
|
||||||
if (shouldNotNotificate) return;
|
if (shouldNotNotificate) return;
|
||||||
|
|
||||||
// show desktop notification
|
handleNotifications(data, history);
|
||||||
const { message, contact, ticket } = data;
|
|
||||||
const options = {
|
|
||||||
body: `${message.body} - ${format(new Date(), "HH:mm")}`,
|
|
||||||
icon: contact.profilePicUrl,
|
|
||||||
tag: ticket.id,
|
|
||||||
};
|
|
||||||
let notification = new Notification(
|
|
||||||
`${i18n.t("tickets.notification.message")} ${contact.name}`,
|
|
||||||
options
|
|
||||||
);
|
|
||||||
|
|
||||||
notification.onclick = function (event) {
|
|
||||||
event.preventDefault(); //
|
|
||||||
window.focus();
|
|
||||||
history.push(`/tickets/${ticket.id}`);
|
|
||||||
};
|
|
||||||
|
|
||||||
document.addEventListener("visibilitychange", () => {
|
|
||||||
if (document.visibilityState === "visible") {
|
|
||||||
notification.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
soundAlert.current.play();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -141,6 +136,41 @@ const NotificationsPopOver = () => {
|
|||||||
};
|
};
|
||||||
}, [history, userId]);
|
}, [history, userId]);
|
||||||
|
|
||||||
|
const handleNotifications = (data, history) => {
|
||||||
|
const { message, contact, ticket } = data;
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
body: `${message.body} - ${format(new Date(), "HH:mm")}`,
|
||||||
|
icon: contact.profilePicUrl,
|
||||||
|
tag: ticket.id,
|
||||||
|
renotify: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
const notification = new Notification(
|
||||||
|
`${i18n.t("tickets.notification.message")} ${contact.name}`,
|
||||||
|
options
|
||||||
|
);
|
||||||
|
|
||||||
|
notification.onclick = e => {
|
||||||
|
e.preventDefault();
|
||||||
|
window.focus();
|
||||||
|
history.push(`/tickets/${ticket.id}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
setDesktopNotifications(prevState => {
|
||||||
|
const notfiticationIndex = prevState.findIndex(
|
||||||
|
n => n.tag === notification.tag
|
||||||
|
);
|
||||||
|
if (notfiticationIndex !== -1) {
|
||||||
|
prevState[notfiticationIndex] = notification;
|
||||||
|
return [...prevState];
|
||||||
|
}
|
||||||
|
return [notification, ...prevState];
|
||||||
|
});
|
||||||
|
|
||||||
|
soundAlertRef.current();
|
||||||
|
};
|
||||||
|
|
||||||
const handleClick = () => {
|
const handleClick = () => {
|
||||||
setIsOpen(prevState => !prevState);
|
setIsOpen(prevState => !prevState);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user