Send a push notification
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. 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)
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:
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.
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.
Encode your private key with Base64
The privateKey value must be base64 encoded.
1curl -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.
Info
The certificate and privateKey values must be base64 encoded.
1curl -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
POSTrequest.
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.
1curl -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
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.
1curl -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
- To send notifications to Android devices and web browsers, set up FCM credentials.
- To send notifications to Apple devices, set up APNs credentials.
- Review the Push Notifications API reference.