This repository contains a Supabase Edge Function for sending push notifications via OneSignal.
- Supabase project
- OneSignal account with an app set up
- OneSignal API Key
- OneSignal App ID
- Clone this repository
- Deploy the edge function to your Supabase project:
supabase functions deploy onesignal-push-notify --project-ref your-project-ref
- Set the required environment variables:
supabase secrets set ONESIGNAL_API_KEY=your-onesignal-api-key --project-ref your-project-ref
supabase secrets set ONESIGNAL_APP_ID=your-onesignal-app-id --project-ref your-project-ref
Note: This edge function uses OneSignal's REST API directly and does not require any OneSignal SDK or additional dependencies to be installed.
For more information about the OneSignal REST API, see the OneSignal API Reference.
The edge function accepts POST requests with a JSON body containing a user_id
parameter.
const { data, error } = await supabase.functions.invoke('onesignal-push-notify', {
body: {
user_id: 'user-123',
title: 'Custom Title', // Optional - defaults to "Test OneSignal Push"
message: 'This is a custom message' // Optional - defaults to a standard message
},
})
Or using fetch:
const response = await fetch('https://your-project-ref.supabase.co/functions/v1/onesignal-push-notify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer your-supabase-anon-key',
},
body: JSON.stringify({
user_id: 'user-123',
title: 'Custom Title', // Optional
message: 'This is a custom message' // Optional
}),
})
The function returns a JSON response with:
success
: boolean indicating if the notification was sent successfullyerror
: error message if the notification failed
- The
user_id
parameter must match the external ID of a user in your OneSignal app - The function automatically converts the user_id to uppercase when sending to OneSignal
- The function accepts optional
title
andmessage
parameters to customize notifications - If not provided, a default title "Test OneSignal Push" and a generic message will be used
If you prefer to use the OneSignal SDK instead of the REST API approach outlined in this repository, check out the official Supabase OneSignal integration guide: https://supabase.com/partners/integrations/onesignal
If you encounter issues with the function, check the Supabase logs:
supabase functions logs onesignal-push-notify --project-ref your-project-ref