Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Send a push notification


(new)

Beta

The Push Notifications API is currently available as a Private Beta product. The information contained in this document is subject to change. You acknowledge and agree that your use of the Twilio Push Notifications API is subject to the terms of the Services in Private Beta(link takes you to an external page). This means that some features are not yet implemented and others may be changed before the product is declared as Generally Available. Private Beta products are not covered by the Twilio Support Terms or Twilio Service Level Agreement.

Mobile devices and browsers rely on platform-specific messaging services to receive push notifications. Twilio supports the following services for delivering push notifications:

  • Android and all web browsers (including Safari): Google Firebase Cloud Messaging (FCM)
  • iOS devices: Apple Push Notification service (APNs)

Resources

resources page anchor

The Push Notifications API uses the following resources:

  • Credentials: Authentication permissions from Google or Apple. After you upload a Credential to Twilio, you can associate it with an App. If you omit the App, Twilio uses your default App.
  • App: Associates different Credentials with specific DeviceRegistrations. Each version, variant, or environment for your app might require different permissions. This might be because a staging server requires different permissions than a production server.

Before you send push notifications, complete the following steps:

  • Obtain credentials from FCM, APN, or both.
  • Upload your credentials to the Comms API.

To separate registrations and credentials for their intended destination, use a namespace called an App.

For example, you might have a development App and a production App, or offer more than one user-facing application to your customers. If you only have one App, Twilio sets it to isDefault: true. Unless you specify otherwise in your request, Credentials and DeviceRegistrations fall under the default App.

When creating your Credential, specify an appName. If the App doesn't exist, the API creates it. If no other Apps exist, the API sets your created App as the default App.

Upload FCM credentials

upload-fcm-credentials page anchor

To upload FCM credentials to Twilio, make a POST request to the Credentials resource including a credential type of FCM, your private key, and your App name.

(information)

Encode your private key with Base64

The privateKey value must be base64 encoded.

Upload FCM credentials using cURL

upload-fcm-credentials-using-curl page anchor
1
curl -X POST 'https://comms.twilio.com/v1/PushNotifications/Credentials' \
2
-H 'Content-Type: application/json' \
3
-d '{
4
"credentialType": "FCM",
5
"content": {
6
"privateKey": "YOUR_PRIVATE_KEY"
7
},
8
"appName": "YOUR_APP_NAME"
9
}' \
10
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

To learn how to generate and encode your credentials, see Obtain FCM credentials.

To upload APNs credentials to Twilio, call the Credentials resource including a credential type of APN, your certificate, your private key, and your App name.

(information)

Info

The certificate and privateKey values must be base64 encoded.

Upload APNs credentials using cURL

upload-apns-credentials-using-curl page anchor
1
curl -X POST 'https://comms.twilio.com/v1/PushNotifications/Credentials' \
2
-H 'Content-Type: application/json' \
3
-d '{
4
"credentialType": "APN",
5
"content": {
6
"certificate": "YOUR_CERTIFICATE",
7
"privateKey": "YOUR_PRIVATE_KEY"
8
},
9
"appName": "YOUR_APP_NAME"
10
}' \
11
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

To learn how to generate and encode your credentials, see Obtain APNs credentials.


You can send a notification without storing it as a registration. In production use cases, you have two options:

  • Store the token on a User with DeviceRegistrations.
  • Manage device tokens yourself and send notifications using a direct POST request.

Send to a single recipient

send-to-a-single-recipient page anchor

The following request sends a notification to one FCM device using your default App and its associated Credentials. To send using a non-default App or a specific Credential, include the from object in the request body. See the PushNotifications resource reference for details.

Single recipient push notification using cURL

single-recipient-push-notification-using-curl page anchor
1
curl -X POST 'https://comms.twilio.com/v1/PushNotifications' \
2
-H 'Content-Type: application/json' \
3
-d '{
4
"to": [
5
{
6
"token": "FCM_DEVICE_TOKEN",
7
"provider": "FCM"
8
}
9
],
10
"content": {
11
"title": "Your flight details",
12
"body": "Hello, your flight is leaving soon! Tap for details"
13
}
14
}' \
15
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

Send to multiple recipients with personalization

send-to-multiple-recipients-with-personalization page anchor

You can send personalized push notifications to up to 10,000 recipients in a single request, with a mix of APNs and FCM recipients. To personalize notifications for each recipient, use the LiquidJS templating language.

Multiple recipient push notification using cURL

multiple-recipient-push-notification-using-curl page anchor
1
curl -X POST 'https://comms.twilio.com/v1/PushNotifications' \
2
-H 'Content-Type: application/json' \
3
-d '{
4
"to": [
5
{
6
"token": "FCM_DEVICE_TOKEN",
7
"provider": "FCM",
8
"variables": {
9
"name": "Jessie"
10
}
11
},
12
{
13
"token": "APN_DEVICE_TOKEN",
14
"provider": "APN",
15
"variables": {
16
"name": "James"
17
}
18
}
19
],
20
"content": {
21
"title": "New styles in stock!",
22
"body": "Hey {{name}}, new colorways just dropped on your favorite silhouettes."
23
}
24
}' \
25
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN