Skip to content

mshivam019/StorySail

Repository files navigation

Expo React Native Push Notifications with Supabase Edge Functions

Expo makes implementing push notifications easy. All the hassle with device information and communicating with Firebase Cloud Messaging (FCM) or Apple Push Notification Service (APNs) is done behind the scenes. This allows you to treat Android and iOS notifications in the same way and save time both on the frontend and backend.

Supabase Setup

Expo Setup

To utilize Expo's push notification service, you must configure your app by installing a set of libraries, implementing functions to handle notifications, and setting up credentials for Android and iOS. Follow the official Expo Push Notifications Setup Guide to get the credentials for Android and iOS. This project uses Expo's EAS build service to simplify this part.

  1. Install the dependencies: npm i
  2. Create a new Expo project
  3. Link this app to your project: npm install --global eas-cli && eas init --id your-expo-project-id
  4. Create a build for your physical device
  5. Start the development server for your project: npx expo start --dev-client
  6. Scan the QR code shown in the terminal with your physical device.
  7. Sign up/in to create a user in Supabase Auth.

Enhanced security for push notifications

  1. Navigate to your Expo Access Token Settings
  2. Create a new token for usage in Supabase Edge Functions
  3. Toggle on "Enhanced Security for Push Notifications"
  4. Create the local .env file: cp .env.local.example .env.local
  5. In the newly created .env.local file, set your EXPO_ACCESS_TOKEN value.

Deploy the Supabase Edge Function

The database webhook handler to send push notifications is located in supabase/functions/push/index.ts. Deploy the function to your linked project and set the EXPO_ACCESS_TOKEN secret.

  1. supabase functions deploy push
  2. supabase secrets set --env-file .env.local

Create the database webhook

Navigate to the Database Webhooks settings in your Supabase Dashboard.

  1. Enable and create a new hook.
  2. Conditions to fire webhook: Select the notifications table and tick the Insert event.
  3. Webhook configuration: Supabase Edge Functions.
  4. Edge Function: Select the push edge function and leave the method as POST and timeout as 1000.
  5. HTTP Headers: Click "Add new header" > "Add auth header with service key and leave Content-type: application/json.
  6. Click "Create webhook".

Send push notification

  1. Navigate to the table editor in your Supabase Dashboard.
  2. In your notifications table, insert a new row.
  3. Watch the magic happen 🪄