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, |
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
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
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 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
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
const pushServerPublicKey = "<A PUSH SERVER PUBLIC KEY GOES HERE>"; | |
/** | |
* | |
* using the registered service worker creates a push notification subscription and returns it | |
* | |
*/ | |
function createNotificationSubscription() { | |
//wait for service worker installation to be ready, and then | |
return navigator.serviceWorker.ready.then(function(serviceWorker) { |
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
/** | |
* checks if Push notification and service workers are supported by your browser | |
*/ | |
function isPushNotificationSupported() { | |
return "serviceWorker" in navigator && "PushManager" in window; | |
} | |
/** | |
* asks user consent to receive push notifications and returns the response of the user, one of granted, default, denied | |
*/ |
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 img = "/images/jason-leung-HM6TMmevbZQ-unsplash.jpg"; | |
const text = "Take a look at this brand new t-shirt!"; | |
const title = "New Product Available"; | |
const options = { | |
body: text, | |
icon: "/images/jason-leung-HM6TMmevbZQ-unsplash.jpg", | |
vibrate: [200, 100, 200], | |
tag: "new-product", | |
image: img, | |
badge: "https://spyna.it/icons/android-icon-192x192.png", |
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 asyncForEach = async (array, callback) => { | |
for (let index = 0; index < array.length; index++) { | |
await callback(array[index], index, array); | |
} | |
} |
NewerOlder