Skip to main content

Direct OneSignal API Push

If you use your own OneSignal app, you can bypass the Cozie hosted API and call the OneSignal API directly. This is useful for testing a custom app build or scheduling a simple push notification without deploying a custom backend first.

For the current OneSignal API parameters, see the OneSignal documentation for sending messages, push notifications, and API authentication.

Prepare the following values:

NameDescription
PROJECT_IDGoogle Cloud project that owns the Cloud Scheduler job
LOCATIONGoogle Cloud Scheduler location, such as australia-southeast1
JOB_NAMEName of the Cloud Scheduler job
APP_IDOneSignal App ID for your custom Cozie app
API_KEYOneSignal REST API key for the app
SUBSCRIPTION_IDOneSignal subscription ID for the target device

You can create a Google Cloud Scheduler HTTP job that calls the OneSignal push notification endpoint directly:

gcloud scheduler jobs create http "${JOB_NAME}" \
--project="${PROJECT_ID}" \
--location="${LOCATION}" \
--schedule="0 11 * * *" \
--time-zone="Australia/Sydney" \
--uri="https://api.onesignal.com/notifications?c=push" \
--http-method=POST \
--headers="Content-Type=application/json,Authorization=Key ${API_KEY}" \
--message-body="{\"app_id\":\"${APP_ID}\",\"target_channel\":\"push\",\"include_subscription_ids\":[\"${SUBSCRIPTION_ID}\"],\"headings\":{\"en\":\"Cozie Test Notification\"},\"contents\":{\"en\":\"Scheduled notification at 11AM.\"}}"

Run the job immediately to test the notification:

gcloud scheduler jobs run "${JOB_NAME}" \
--project="${PROJECT_ID}" \
--location="${LOCATION}"
warning

Do not commit OneSignal REST API keys to Git. Cloud Scheduler also stores the configured HTTP headers with the job, so restrict access to the Google Cloud project and scheduler job. For production deployments, consider sending notifications through a controlled backend or storing secrets in Google Cloud Secret Manager.