-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to latest version of SDK and fix issues
- Loading branch information
1 parent
9ffad59
commit aa990ab
Showing
9 changed files
with
104 additions
and
92 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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> | ||
</> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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,6 @@ | ||
import * as OneSignal from '@onesignal/node-onesignal' | ||
|
||
const configuration = OneSignal.createConfiguration({ | ||
restApiKey: process.env['API_KEY']!, | ||
}) | ||
export const OneSignalClient = new OneSignal.DefaultApi(configuration) |
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