Skip to content

Instantly share code, notes, and snippets.

@Spyna
Created August 30, 2019 07:52
Show Gist options
  • Save Spyna/0741bf0f0ed180e344f09939f3ba5c6c to your computer and use it in GitHub Desktop.
Save Spyna/0741bf0f0ed180e344f09939f3ba5c6c to your computer and use it in GitHub Desktop.
A React Component that uses the usePushNotificationHook
import React from "react";
import usePushNotifications from "./usePushNotifications";
export default function PushNotificationDemo() {
const {
userConsent,
pushNotificationSupported,
userSubscription,
onClickAskUserPermission,
onClickSusbribeToPushNotification,
onClickSendSubscriptionToPushServer,
pushServerSubscriptionId,
onClickSendNotification,
error,
loading
} = usePushNotifications();
return (
<div>
{error && (
<section className="app-error">
<h2>{error.name}</h2>
<p>Error message : {error.message}</p>
<p>Error code : {error.code}</p>
</section>
)}
{loading && "Loading, please stand by"}
<p>Push notification are {!pushNotificationSupported && "NOT"} supported by your device.</p>
<p>
User consent to recevie push notificaitons is <strong>{userConsent}</strong>.
</p>
<button onClick={onClickAskUserPermission}>Ask user permission</button>
<button onClick={onClickSusbribeToPushNotification}>Create Notification subscription</button>
<button onClick={onClickSendSubscriptionToPushServer}>Send subscription to push server</button>
<button onClick={onClickSendNotification}>Send a notification</button>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment