Skip to content

Commit

Permalink
Update to latest version of SDK and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
iAmWillShepherd committed Dec 2, 2024
1 parent 9ffad59 commit aa990ab
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 92 deletions.
11 changes: 0 additions & 11 deletions common/actions.ts

This file was deleted.

10 changes: 0 additions & 10 deletions common/onesignal.ts

This file was deleted.

82 changes: 74 additions & 8 deletions components/Demo.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,93 @@
'use client'
import { useEffect, useState } from 'react'
import OneSignal from 'react-onesignal'

import { sendUserNotification } from '@common/actions'
import useOneSignal from '@hooks/onesignal'

// const userId = 'test'
export default function Demo() {
const { userId } = useOneSignal()
const [subscriptionId, setSubscriptionId] = useState<string | null>(null)
const [alreadyInitialized, setAlreadyInitialized] = useState(false)

useEffect(() => {
if (alreadyInitialized) return

const init = async () => {
try {
console.log('Initializing OneSignal')
await OneSignal.init({
appId: process.env['NEXT_PUBLIC_APP_ID']!,
allowLocalhostAsSecureOrigin: true,
notifyButton: {
enable: true,
size: 'large',
},
})

OneSignal.User.PushSubscription.addEventListener('change', event => {
event.current.id
? setSubscriptionId(event.current.id)
: setSubscriptionId(null)
})
OneSignal.Notifications.addEventListener(
'foregroundWillDisplay',
event => {
console.info('Notification willDisplay', event)
}
)

setAlreadyInitialized(true)
} catch (e) {
console.error('OneSignal initilization error.', e)
}
}
window && init()
}, [])

return (
<>
<p className="justify-center mx-auto">
OneSignal User ID: {userId || 'Anonymous User'}
<p className="justify-center mx-auto text-xl">
Subscription ID:{' '}
<span className="font-mono bg-gray-800 text-lime-400 p-1">
{subscriptionId || 'Anonymous'}
</span>
</p>

<button
onClick={e => {
e.preventDefault()
userId && void sendUserNotification(userId)

if (!subscriptionId) return

try {
const json = JSON.stringify({
subscriptionId,
})
fetch('/api/notify', {
method: 'POST',
body: json,
})
} catch (e) {
console.error('Error', e)
}
}}
className="p-2 border border-slate-50 w-48 justify-center mx-auto hover:border-red-500"
>
Send notification...
</button>

<button
className="p-2 border border-slate-50 w-48 justify-center mx-auto hover:border-red-500"
onClick={async e => {
e.preventDefault()
console.log('Launching prompt')

await OneSignal.Slidedown.promptPush({
force: true,
forceSlidedownOverNative: true,
})
console.log('Launched prompt')
}}
>
Launch prompt
</button>
</>
)
}
46 changes: 0 additions & 46 deletions hooks/onesignal.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "next dev --port 4000",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@onesignal/node-onesignal": "^1.0.0-beta9",
"@onesignal/node-onesignal": "^5.0.0-alpha-01",
"@types/node": "18.11.19",
"@types/react": "18.0.27",
"@types/react-dom": "18.0.10",
Expand Down
6 changes: 6 additions & 0 deletions pages/api/common/onesignal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as OneSignal from '@onesignal/node-onesignal'

const configuration = OneSignal.createConfiguration({
restApiKey: process.env['API_KEY']!,
})
export const OneSignalClient = new OneSignal.DefaultApi(configuration)
10 changes: 5 additions & 5 deletions pages/api/notify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { appId, OneSignalClient } from "@/common/onesignal"
import type { NextApiRequest, NextApiResponse } from "next"
import { OneSignalClient } from '@/pages/api/common/onesignal'
import type { NextApiRequest, NextApiResponse } from 'next'

type Data = {
message: string
Expand All @@ -12,16 +12,16 @@ export default async function handler(
try {
const body = JSON.parse(req.body)
const apiRes = await OneSignalClient.createNotification({
app_id: appId,
include_player_ids: [body.userId],
app_id: '',
include_aliases: { external_id: [body.subscriptionId] },
contents: {
en: "You've been notified!",
},
})

res
.status(200)
.send({ message: `Notification ${apiRes.id} sent to user ${""}` })
.send({ message: `Notification ${apiRes.id} sent to user ${''}` })
} catch (err) {
console.error(`Fatal error - ${err}`)
res.status(500).send({ message: JSON.stringify(err) })
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"baseUrl": ".",
"paths": {
"@/*": ["./*"],
"@common/*": ["./common/*"],
"@common/*": ["pages/api/common/*"],
"@hooks/*": ["./hooks/*"],
"@components/*": ["./components/*"]
}
Expand Down
25 changes: 16 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@onesignal/node-onesignal@^1.0.0-beta9":
version "1.0.0-beta9"
resolved "https://registry.yarnpkg.com/@onesignal/node-onesignal/-/node-onesignal-1.0.0-beta9.tgz#271f007771c6505f78214393e710c98ca10a2a38"
integrity sha512-i9ovWyMG6KxdLp7s/gsFUWa62ic723dj0N+LCYYzAcMpRZJOkoU8/en9BjrQOElTPk/4wpfg4puz4qNgPfL8fQ==
"@onesignal/node-onesignal@^5.0.0-alpha-01":
version "5.0.0-alpha-01"
resolved "https://registry.yarnpkg.com/@onesignal/node-onesignal/-/node-onesignal-5.0.0-alpha-01.tgz#4419e11c58c3208eaede3f88102c477b9eafc186"
integrity sha512-Ed4CwHk6gh7iaettBQ6DLszCWugbT2ldts26HMmcC4b0GQ43riheKpXrf8yuezZppwFF77nYyHOE/JEyRyBBsg==
dependencies:
"@types/node" "^18.11.18"
"@types/node" "^20.12.8"
"@types/node-fetch" "^2.5.7"
btoa "^1.2.1"
es6-promise "^4.2.4"
Expand Down Expand Up @@ -223,10 +223,12 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.19.tgz#35e26df9ec441ab99d73e99e9aca82935eea216d"
integrity sha512-YUgMWAQBWLObABqrvx8qKO1enAvBUdjZOAWQ5grBAkp5LQv45jBvYKZ3oFS9iKRCQyFjqw6iuEa1vmFqtxYLZw==

"@types/node@^18.11.18":
version "18.16.13"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.16.13.tgz#984c48275c718b5b3e47371938f3cff482790598"
integrity sha512-uZRomboV1vBL61EBXneL4j9/hEn+1Yqa4LQdpGrKmXFyJmVfWc9JV9+yb2AlnOnuaDnb2PDO3hC6/LKmzJxP1A==
"@types/node@^20.12.8":
version "20.17.9"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.17.9.tgz#5f141d4b7ee125cdee5faefe28de095398865bab"
integrity sha512-0JOXkRyLanfGPE2QRCwgxhzlBAvaRdCNMcvbd7jFfpmD4eEXll7LRwy5ymJmyeZqk7Nh7eD2LeUyQ68BbndmXw==
dependencies:
undici-types "~6.19.2"

"@types/prop-types@*":
version "15.7.5"
Expand Down Expand Up @@ -2620,6 +2622,11 @@ unbox-primitive@^1.0.2:
has-symbols "^1.0.3"
which-boxed-primitive "^1.0.2"

undici-types@~6.19.2:
version "6.19.8"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02"
integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==

untildify@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/untildify/-/untildify-4.0.0.tgz#2bc947b953652487e4600949fb091e3ae8cd919b"
Expand Down

0 comments on commit aa990ab

Please sign in to comment.