Skip to content

Commit

Permalink
chore: register on web load
Browse files Browse the repository at this point in the history
  • Loading branch information
devceline committed Mar 8, 2024
1 parent 32cafe7 commit 5b94393
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/contexts/W3iContext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useAllSubscriptions, useWeb3InboxAccount, useWeb3InboxClient } from '@w

import W3iContext from '@/contexts/W3iContext/context'
import { useAuthState } from '@/contexts/W3iContext/hooks/authHooks'
import { setupSubscriptionsSymkeys } from '@/utils/notifications'
import { registerWithEcho, setupSubscriptionsSymkeys } from '@/utils/notifications'

interface W3iContextProviderProps {
children: React.ReactNode | React.ReactNode[]
Expand All @@ -21,6 +21,13 @@ const W3iContextProvider: React.FC<W3iContextProviderProps> = ({ children }) =>
setupSubscriptionsSymkeys(activeSubscriptions?.map(sub => [sub.topic, sub.symKey]) ?? [])
}, [activeSubscriptions])

useEffect(() => {
// register on client init
if(client && window?.Notification.permission === 'granted') {
registerWithEcho(client)
}
}, [client])

return (
<W3iContext.Provider
value={{
Expand Down
6 changes: 6 additions & 0 deletions src/utils/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ export const requireNotifyPermission = async () => {
}

export const registerWithEcho = async (client: Web3InboxClient) => {
console.log(">>> Registering with echo...")
if (await requireNotifyPermission()) {
console.log(">>> Got permission")
const token = await getFirebaseToken()
console.log(">>> Got token", token)

const clientId = await client.registerWithPushServer(token, 'fcm')
console.log(">>> Got clientId", clientId)

const [, putRegistrationToken ] = await getDbEchoRegistrations()

await putRegistrationToken(clientId, token)

console.log(">>> put token")
}
}

0 comments on commit 5b94393

Please sign in to comment.