-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from indexnetwork/dev
Iterate
- Loading branch information
Showing
15 changed files
with
302 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
{ | ||
"name": "Index Network", | ||
"short_name": "Index", | ||
"start_url": "/", | ||
"display": "standalone", | ||
"background_color": "#black", | ||
"theme_color": "#90cdf4", | ||
"orientation": "portrait-primary", | ||
"icons": [ | ||
{ | ||
"src": "favicon-white.png", | ||
"sizes": "500x500", | ||
"type": "image/png" | ||
}, | ||
{ | ||
"src": "favicon-white.png", | ||
"sizes": "192x192", | ||
"type": "image/png" | ||
} | ||
] | ||
} | ||
"name":"Index Network", | ||
"short_name":"Index", | ||
"start_url":"/notifications", | ||
"display":"standalone", | ||
"background_color":"black", | ||
"theme_color":"black", | ||
"orientation":"portrait-primary", | ||
"icons":[ | ||
{ | ||
"src":"favicon-white.png", | ||
"sizes":"500x500", | ||
"type":"image/png" | ||
}, | ||
{ | ||
"src":"favicon-white.png", | ||
"sizes":"192x192", | ||
"type":"image/png" | ||
} | ||
], | ||
"permissions": ["notifications"] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
importScripts('https://assets.magicbell.io/web-push-notifications/sw.js'); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"use client"; | ||
|
||
import type { NextPage } from "next"; | ||
import { useState, useEffect } from "react"; | ||
import { WebPushClient } from "@magicbell/webpush"; | ||
import Head from "next/head"; | ||
import { useApi } from "@/context/APIContext"; | ||
import { useAuth } from "@/context/AuthContext"; | ||
|
||
const Notifications: NextPage = () => { | ||
const [client, setClient] = useState<WebPushClient | null>(null); | ||
const [isSubscribed, setIsSubscribed] = useState(false); | ||
const { api: apiService, ready: apiReady } = useApi(); | ||
const { session, userDID } = useAuth(); | ||
useEffect(() => { | ||
if (!apiReady) return; | ||
if (!session) return; | ||
|
||
const initializeWebPushClient = async () => { | ||
if ("serviceWorker" in navigator) { | ||
try { | ||
// Service Worker'ı kaydet | ||
const registration = await navigator.serviceWorker.register("/sw.js"); | ||
console.log("Service Worker registered successfully:", registration); | ||
|
||
// Service Worker'ın aktif olmasını bekle | ||
await navigator.serviceWorker.ready; | ||
console.log("Service Worker is now ready"); | ||
|
||
const profile = await apiService?.getCurrentProfile(); | ||
if (!profile) { | ||
return; | ||
} | ||
|
||
console.log(profile); | ||
|
||
const webPushClient = new WebPushClient({ | ||
apiKey: process.env.MAGICBELL_API_KEY || "", | ||
userExternalId: profile?.id, | ||
userHmac: profile.hmac!, | ||
serviceWorkerPath: "/sw.js", | ||
}); | ||
|
||
setClient(webPushClient); | ||
|
||
const subscribed = await webPushClient.isSubscribed(); | ||
setIsSubscribed(subscribed); | ||
} catch (error) { | ||
console.error("Error initializing WebPush:", error); | ||
} | ||
} else { | ||
console.error("Service Workers are not supported in this browser"); | ||
} | ||
}; | ||
|
||
initializeWebPushClient(); | ||
}, [session, apiReady]); | ||
|
||
const handleSubscribe = async () => { | ||
if (client) { | ||
try { | ||
await client.subscribe(); | ||
setIsSubscribed(true); | ||
} catch (error) { | ||
console.error("Subscription failed:", error); | ||
} | ||
} | ||
}; | ||
|
||
const handleUnsubscribe = async () => { | ||
if (client) { | ||
try { | ||
await client.unsubscribe(); | ||
setIsSubscribed(false); | ||
} catch (error) { | ||
console.error("Unsubscription failed:", error); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h1>Web Push Notifications</h1> | ||
{isSubscribed ? ( | ||
<button onClick={handleUnsubscribe}>Bildirimleri Kapat</button> | ||
) : ( | ||
<button onClick={handleSubscribe}>Bilaadirimleri Aç</button> | ||
)} | ||
<Head> | ||
<link rel="manifest" href="/manifest.json"/> | ||
</Head> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Notifications; |
This file contains 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
This file contains 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
Oops, something went wrong.