Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

adds OneSignal guide

dshukertjr9befb1d98e8b
diff --git a/apps/docs/pages/guides/integrations/onesignal.mdx b/apps/docs/pages/guides/integrations/onesignal.mdx
new file mode 100644
index 00000000000..bed855a7a4b
--- /dev/null
+++ b/apps/docs/pages/guides/integrations/onesignal.mdx
@@ -0,0 +1,317 @@
+import Layout from '~/layouts/DefaultGuideLayout'
+
+export const meta = {
+ id: 'onesignal',
+ title: 'OneSignal',
+ description:
+ 'OneSignal allows you to send cloud messages to your users. Combine OneSignal with your Supabase apps and you can reach out to your users whenever there is a change in your database.',
+}
+
+OneSignal is a tool that allows you to send messages across different channels such as the following to keep your users engaged.
+
+- Push notifications
+- SMS
+- Emails
+- In-app notifications
+
+This guide steps you through how you can integrate OneSignal with Supabase to create a seamless cloud messaging experience for your users using Database webhooks and edge functions through a simple Next.js application.
+
+We will have a simple UI and use Supabase’s Database Webhooks in conjunction with Edge Function to make a real-time push notification experience.
+
+You can find the complete example app along with the edge functions code to send the notifications [here](https://github.com/supabase-community/onesignal).
+
+![Screen Shot 2022-12-14 at 0.17.03.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e8f411e8-456c-466c-b26d-389687ada880/Screen_Shot_2022-12-14_at_0.17.03.png)
+
+## Step 1: Getting started
+
+Before we dive into the code, this guide assumes that you have the following ready
+
+- [Supabase](https://supabase.com/) project created
+- [OneSignal](https://onesignal.com/) app created
+- [Supabase CLI](https://supabase.com/docs/guides/cli) installed on your machine
+
+Let’s create a Next.js app with tailwind CSS pre-installed
+
+```bash
+npx create-next-app -e with-tailwindcss --ts
+```
+
+We will then install the Supabase and OneSignal SDK.
+
+```bash
+npm i @supabase/supabase-js
+npm i react-onesignal
+```
+
+After that, follow the instructions [here](https://documentation.onesignal.com/docs/web-push-custom-code-setup) to set up OneSignal for the web. You can set the URL of the app as a local host if you want to run the app locally, or add a remote URL if you want to deploy your app to a public hosting. You should add the file you obtain in step 4 of the instruction under the `public` directory of your Next.js app like [this](https://github.com/supabase-community/onesignal/tree/main/app/public).
+
+## Step 2: Build Next.js app
+
+The Next.js app will have a login form for the user to sign in, and a button that they can press to make an order once they are signed in. Update the `index.tsx` file to the following.
+
+```tsx
+import { createClient, SupabaseClient, User } from '@supabase/supabase-js'
+import type { NextPage } from 'next'
+import Head from 'next/head'
+import React, { useEffect, useState } from 'react'
+import OneSignal from 'react-onesignal'
+
+const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!
+const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
+const oneSignalAppId = process.env.NEXT_PUBLIC_ONESIGNAL_APP_ID!
+
+const supabase = createClient(supabaseUrl, supabaseAnonKey)
+
+const Home: NextPage = () => {
+ const [user, setUser] = useState<User | null>(null)
+
+ const [oneSignalInitialized, setOneSignalInitialized] =
+ useState<boolean>(false)
+
+ const initializeOneSignal = async (uid: string) => {
+ if (oneSignalInitialized) {
+ return
+ }
+ setOneSignalInitialized(true)
+ await OneSignal.init({
+ appId: oneSignalAppId,
+ notifyButton: {
+ enable: true,
+ },
+
+ allowLocalhostAsSecureOrigin: true,
+ })
+
+ await OneSignal.setExternalUserId(uid)
+ }
+
+ useEffect(() => {
+ const initialize = async () => {
+ const initialUser = (await supabase.auth.getUser())?.data.user
+ setUser(initialUser ?? null)
+ if (initialUser) {
+ initializeOneSignal(initialUser.id)
+ }
+ }
+
+ initialize()
+
+ const authListener = supabase.auth.onAuthStateChange(
+ async (event, session) => {
+ const user = session?.user ?? null
+ setUser(user)
+ if (user) {
+ initializeOneSignal(user.id)
+ }
+ }
+ )
+
+ return () => {
+ authListener.data.subscription.unsubscribe()
+ }
+ }, [])
+
+ const sendMagicLink = async (event: React.FormEvent<HTMLFormElement>) => {
+ event.preventDefault()
+ const { email } = Object.fromEntries(new FormData(event.currentTarget))
+ if (typeof email !== 'string') return
+
+ const { error } = await supabase.auth.signInWithOtp({ email })
+ if (error) {
+ alert(error.message)
+ } else {
+ alert('Check your email inbox')
+ }
+ }
+
+ // Create a order with price of $100
+ const submitOrder = async () => {
+ const { error } = await supabase.from('orders').insert({
+ price: 100,
+ })
+ if (error) {
+ alert(error.message)
+ }
+ }
+
+ return (
+ <>
+ <Head>
+ <title>OneSignal Order Notification App</title>
+ <link rel="icon" href="/favicon.ico" />
+ </Head>
+
+ <main className="flex items-center justify-center min-h-screen">
+ {user ? (
+ <div>
+ <button
+ onClick={submitOrder}
+ className="py-1 px-4 text-lg bg-green-400 rounded"
+ >
+ Place an Order
+ </button>
+ </div>
+ ) : (
+ <form className="flex flex-col space-y-2" onSubmit={sendMagicLink}>
+ <input
+ className="border-green-300 border rounded p-2"
+ type="email"
+ name="email"
+ placeholder="Email"
+ />
+ <button
+ type="submit"
+ className="py-1 px-4 text-lg bg-green-400 rounded"
+ >
+ Send Magic Link
+ </button>
+ </form>
+ )}
+ </main>
+ </>
+ )
+}
+
+export default Home
+```
+
+There is quite a bit of stuff going on here, but basically, it’s creating a simple UI for the user to sign in using the [magic link](https://supabase.com/docs/guides/auth/auth-magic-link), and once the user is signed in, will initialize OneSignal to ask the user to receive notifications on the website.
+
+Notice that inside the `initializeOneSignal()` function, we are setting the Supabase user ID as an [external user ID of OneSignal](https://documentation.onesignal.com/docs/external-user-ids). This allows us to later send push notifications to the user using their Supabase user ID from the backend, which is very handy.
+
+```tsx
+await OneSignal.setExternalUserId(uid);
+```
+
+The front-end side of things is done here. Let’s get into the backend.
+
+We also need to set our environment variables. Create a `.env.local` file and use the following template to set the environment variables. You can find your Supabase configuration in your dashboard under `settings > API`, and you can find the OneSignal app ID from `Settings > Keys & IDs`
+
+```bash
+NEXT_PUBLIC_SUPABASE_URL=YOUR_SUPABASE_URL
+NEXT_PUBLIC_SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEY
+NEXT_PUBLIC_ONESIGNAL_APP_ID=YOUR_ONESIGNAL_APP_ID
+```
+
+![Screen Shot 2022-12-14 at 0.39.24.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4ea80251-3271-495a-a6e0-f68466403503/Screen_Shot_2022-12-14_at_0.39.24.png)
+
+## Step 3: Create the Edge Function
+
+Let’s create an edge function that will receive [database webhooks](https://supabase.com/docs/guides/database/webhooks) from the database and calls the OneSignal API to send the push notification.
+
+```bash
+supabase functions new notify
+```
+
+Replace the contents of `supabase/functions/notify/index.ts` with the following
+
+```tsx
+import { serve } from 'https://deno.land/[email protected]/http/server.ts'
+import * as OneSignal from 'https://esm.sh/@onesignal/[email protected]'
+
+const _OnesignalAppId_ = Deno.env.get('ONESIGNAL_APP_ID')!
+const _OnesignalUserAuthKey_ = Deno.env.get('USER_AUTH_KEY')!
+const _OnesignalRestApiKey_ = Deno.env.get('ONESIGNAL_REST_API_KEY')!
+const configuration = OneSignal.createConfiguration({
+ userKey: _OnesignalUserAuthKey_,
+ appKey: _OnesignalRestApiKey_,
+})
+
+const onesignal = new OneSignal.DefaultApi(configuration)
+
+serve(async (req) => {
+ try {
+ const { record } = await req.json()
+
+ // Build OneSignal notification object
+ const notification = new OneSignal.Notification()
+ notification.app_id = _OnesignalAppId_
+ notification.include_external_user_ids = [record.user_id]
+ notification.contents = {
+ en: `You just spent $${record.price}!`,
+ }
+ const onesignalApiRes = await onesignal.createNotification(notification)
+
+ return new Response(
+ JSON.stringify({ onesignalResponse: onesignalApiRes }),
+ {
+ headers: { 'Content-Type': 'application/json' },
+ }
+ )
+ } catch (err) {
+ console.error('Failed to create OneSignal notification', err)
+ return new Response('Server error.', {
+ headers: { 'Content-Type': 'application/json' },
+ status: 400,
+ })
+ }
+})
+```
+
+The function receives a `record` object, which is the row inserted in your `orders` table, and constructs a notification object to then send to OneSignal to deliver the push notification.
+
+We also need to set the environment variable for the function. Create a `.env` file under your `supabase` directory and paste the following.
+
+```bash
+ONESIGNAL_APP_ID=YOUR_ONESIGNAL_APP_ID
+USER_AUTH_KEY=YOUR_USER_AUTH_KEY
+ONESIGNAL_REST_API_KEY=YOUR_ONESIGNAL_REST_API_KEY
+```
+
+`ONESIGNAL_APP_ID` and `ONESIGNAL_REST_API_KEY` can be found under `Settings > Keys & IDs` of your OneSignal app, and `USER_AUTH_KEY` can be found by going to `Account & API Keys` page by clicking your icon in the top right corner and scrolling to the `User Auth Key` section.
+
+![Screen Shot 2022-12-14 at 0.39.03.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/02c4f84d-2b0b-49ec-91b8-0da2c3ccc9ca/Screen_Shot_2022-12-14_at_0.39.03.png)
+
+Once your environment variables are filled in, you can run the following command to set the environment variable.
+
+```bash
+supabase secrets set --env-file ./supabase/.env
+```
+
+At this point, the function should be ready to be deployed! Run the following command to deploy your functions to the edge! The `no-verify-jwt` flag is required if you plan to call the function from a webhook.
+
+```bash
+supabase functions deploy notify --no-verify-jwt
+```
+
+## Step 4: Setting up the Supabase database
+
+Finally, we get to set up the database! Run the following SQL to set up the `orders` table.
+
+```sql
+create table if not exists public.orders (
+ id uuid not null primary key default uuid_generate_v4(),
+ created_at timestamptz not null default now(),
+ user_id uuid not null default auth.uid(),
+ price int8 not null
+);
+```
+
+As you can see, the `orders` table has 4 columns and 3 of them have default values. All we need to send from the front-end app is the price. That is why our insert statement looked very simple.
+
+```tsx
+const { error } = await supabase.from('orders').insert({
+ price: 100,
+ })
+```
+
+Let’s also set up the webhook so that whenever a new row is inserted in the `orders` table, it calls the edge function. Go to `Database > Webhooks` and create a new Database Webhook. The table should be set to `orders` and Events should be inserted. The type should be HTTP Request, the HTTP method should be POST, and the URL should be the URL of your edge function. Hit confirm to save the webhook configuration.
+
+![Screen Shot 2022-12-14 at 0.13.52.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/545e22f9-fc66-437d-8a16-6d2226a08192/Screen_Shot_2022-12-14_at_0.13.52.png)
+
+At this point, the app should be complete! Run your app locally with `npm run dev`, or deploy your app to a hosting service and see how you receive a push notification when you place an order!
+
+![Screen Shot 2022-12-14 at 0.17.03.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ec6e3b60-b024-4172-9b9b-35fb2b123499/Screen_Shot_2022-12-14_at_0.17.03.png)
+
+## Resources
+
+This particular example was using Next.js, but you can apply the same principles to implement send push notification, SMS, Emails, and in-app-notifications on other platforms as well.
+
+- [OneSignal + Flutter + Supabase example](https://github.com/OneSignalDevelopers/onesignal-supabase-sample-integration-supabase)
+- [OneSignal Mobile Quickstart](https://documentation.onesignal.com/docs/mobile-sdk-setup)
+- [OneSignal Documentation](https://documentation.onesignal.com/docs/onesignal-platform)
+- [OneSignal Onboarding guide](https://documentation.onesignal.com/docs/onboarding-with-onesignal)
+
+export const Page = ({ children }) => <Layout meta={meta} children={children} />
+
+export default Page