Hook - Notification

원종서·2021년 8월 25일
0

hook

목록 보기
11/11
import {useEffect, useRef, useState, React} from "react";

const useNotification = (title, options) => {
    if (!("Notification" in window)) {
        return;
    }
    const fileNotif = () => {
        if (Notification.permission !== "granted") {
            Notification.requestPermission().then((permission) => {
                if (permission === "granted") {
                    new Notification(title, options);
                } else {
                    return;
                }
            });
        } else {
            new Notification(title, options);
        }
    };
    return fileNotif;
};

export default () => {
    const triggerNotif = useNotification("Hello", {body: "this is options"});
    return (
        <div className="App">
            <button onClick={triggerNotif}>Hi</button>
        </div>
    );
};

0개의 댓글