This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function receivePushNotification(event) { | |
console.log("[Service Worker] Push Received."); | |
//const { image, tag, url, title, text } = event.data.json(); | |
const notificationText = event.data.text(); | |
const title = "A brand new notification!" | |
const options = { | |
//data: url, | |
data: "something you want to send within the notification, such an URL to open" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function openPushNotification(event) { | |
console.log("[Service Worker] Notification click Received.", event.notification.data); | |
event.notification.close(); | |
event.waitUntil(clients.openWindow(event.notification.data)); | |
} | |
self.addEventListener("notificationclick", openPushNotification); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pushServerPublicKey = "BIN2Jc5Vmkmy-S3AUrcMlpKxJpLeVRAfu9WBqUbJ70SJOCWGCGXKY-Xzyh7HDr6KbRDGYHjqZ06OcS3BjD7uAm8"; | |
/** | |
* checks if Push notification and service workers are supported by your browser | |
*/ | |
function isPushNotificationSupported() { | |
return "serviceWorker" in navigator && "PushManager" in window; | |
} | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function receivePushNotification(event) { | |
console.log("[Service Worker] Push Received."); | |
const { image, tag, url, title, text } = event.data.json(); | |
const options = { | |
data: url, | |
body: text, | |
icon: image, | |
vibrate: [200, 100, 200], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from "react"; | |
import http from "./utils/http"; | |
//the function to call the push server: https://github.com/Spyna/push-notification-demo/blob/master/front-end-react/src/utils/http.js | |
import { | |
isPushNotificationSupported, | |
askUserPermission, | |
registerServiceWorker, | |
createNotificationSubscription, | |
getUserSubscription |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import usePushNotifications from "./usePushNotifications"; | |
export default function PushNotificationDemo() { | |
const { | |
userConsent, | |
pushNotificationSupported, | |
userSubscription, | |
onClickAskUserPermission, | |
onClickSusbribeToPushNotification, |
OlderNewer