Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now

Menu

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Notification Resource

The Notification resource sends a notification to existing Bindings or a list of addresses.

You can notify users over these channels:

  • APNS for iOS devices
  • FCM and GCM for Android devices
  • SMS and MMS
  • Facebook Messenger

Make sure you have consent from users before storing their device's address.

It’s a good practice to obtain your end users’ consent before you send them messages and some jurisdictions might require it by law.

We recommend that you consult with your legal counsel to make sure that your communications comply with all applicable laws.

To make sure your messages reach the right people, you should make sure that they have given you their consent to send them messages and that their contact information is current.

Check out the Twilio Marketplace for Add-ons from our partners that can help you keep your database up to date.

Notification properties

Names in PHP format
sid
sid<NO> Not PII

The unique string that we created to identify the Notification resource.

accountSid
sid<AC> Not PII

The SID of the Account that created the Notification resource.

serviceSid
sid<IS> Not PII

The SID of the Service the resource is associated with.

dateCreated
date_time<iso8601> Not PII

The date and time in GMT when the resource was created specified in RFC 2822 format.

identities

The list of identity values of the Users to notify. We will attempt to deliver notifications only to Bindings with an identity in this list.

tags

The tags that select the Bindings to notify. Notifications will be attempted only to Bindings that have all of the tags listed in this property.

segments
string[] Not PII

The list of Segments to notify. The Segment resource is deprecated. Use the tags property, instead.

priority
enum:priority Not PII

The priority of the notification. Can be: low or high and the default is high. A value of low optimizes the client app's battery consumption; however, notifications may be delivered with unspecified delay. For FCM and GCM, low priority is the same as Normal priority. For APNS low priority is the same as 5. A value of high sends the notification immediately, and can wake up a sleeping device. For FCM and GCM, high is the same as High priority. For APNS, high is a priority 10. SMS does not support this property.

ttl
integer Not PII

How long, in seconds, the notification is valid. Can be an integer between 0 and 2,419,200, which is 4 weeks, the default and the maximum supported time to live (TTL). Delivery should be attempted if the device is offline until the TTL elapses. Zero means that the notification delivery is attempted immediately, only once, and is not stored for future delivery. SMS does not support this property.

title
string Not PII

The notification title. For FCM and GCM, this translates to the data.twi_title value. For APNS, this translates to the aps.alert.title value. SMS does not support this property. This field is not visible on iOS phones and tablets but appears on Apple Watch and Android devices.

body

The notification text. For FCM and GCM, translates to data.twi_body. For APNS, translates to aps.alert.body. For SMS, translates to body. SMS requires either this body value, or media_urls attribute defined in the sms parameter of the notification.

sound
string Not PII

The name of the sound to be played for the notification. For FCM and GCM, this Translates to data.twi_sound. For APNS, this translates to aps.sound. SMS does not support this property.

action
string Not PII

The actions to display for the notification. For APNS, translates to the aps.category value. For GCM, translates to the data.twi_action value. For SMS, this parameter is not supported and is omitted from deliveries to those channels.

data
object Not PII

The custom key-value pairs of the notification's payload. For FCM and GCM, this value translates to data in the FCM and GCM payloads. FCM and GCM reserve certain keys that cannot be used in those channels. For APNS, attributes of data are inserted into the APNS payload as custom properties outside of the aps dictionary. In all channels, we reserve keys that start with twi_ for future use. Custom keys that start with twi_ are not allowed and are rejected as 400 Bad request with no delivery attempted. For SMS, this parameter is not supported and is omitted from deliveries to those channels.

apn
object Not PII

The APNS-specific payload that overrides corresponding attributes in the generic payload for APNS Bindings. This property maps to the APNS Payload item, therefore the aps key must be used to change standard attributes. Adds custom key-value pairs to the root of the dictionary. See the APNS documentation for more details. We reserve keys that start with twi_ for future use. Custom keys that start with twi_ are not allowed.

gcm
object Not PII

The GCM-specific payload that overrides corresponding attributes in the generic payload for GCM Bindings. This property maps to the root JSON dictionary. Target parameters to, registration_ids, and notification_key are not allowed. We reserve keys that start with twi_ for future use. Custom keys that start with twi_ are not allowed.

fcm
object Not PII

The FCM-specific payload that overrides corresponding attributes in the generic payload for FCM Bindings. This property maps to the root JSON dictionary. See the FCM documentation for more details. Target parameters to, registration_ids, condition, and notification_key are not allowed in this parameter. We reserve keys that start with twi_ for future use. Custom keys that start with twi_ are not allowed. FCM also reserves certain keys, which cannot be used in that channel.

sms
object Not PII

The SMS-specific payload that overrides corresponding attributes in the generic payload for SMS Bindings. Each attribute in this value maps to the corresponding form parameter of the Twilio Message resource. These parameters of the Message resource are supported in snake case format: body, media_urls, status_callback, and max_price. The status_callback parameter overrides the corresponding parameter in the messaging service, if configured. The media_urls property expects a JSON array.

facebookMessenger
object Not PII

Deprecated.

alexa
object Not PII

Deprecated.

Create a Notification resource

post
https://notify.twilio.com/v1/Services/{ServiceSid}/Notifications

Creating a Notification resource sends a notification to existing Bindings or to addresses provided in the request.

Send a Notification to existing Bindings

Set Identity or Tag to specify the stored Bindings to notify. To notify all available stored Bindings, set Tag to all. If both Identity and Tag have values, only the Bindings that match both will be notified.

  • Example 1 shows how to send a notification to a Binding identified by its identity.
  • Example 2 shows how to send a notification to a Binding identified by its tags.
  • Example 3 shows how to send a notification to a Binding identified by its identity and its tags.

Send a Notification to Bindings in the request

Set ToBinding to the JSON object that defines the Bindings to notify.

  • Example 4 shows how to send a notification to Bindings defined in the request.

Send a Notification tailored to the specific channel

Define channel-specific features in the request to tailor the notification to the channel.

  • Example 5 below shows how to apply channel-specific features to customize the notification content to the channel.
Parameters
Names in None format
service_sid
Required
post sid<IS> Not PII

The SID of the Service to create the resource under.

identity
Required if tag is not passed
post string[] Not PII

The identity value that uniquely identifies the new resource's User within the Service. Delivery will be attempted only to Bindings with an Identity in this list. No more than 20 items are allowed in this list.

tag
Required if identity is not passed
post string[] Not PII

A tag that selects the Bindings to notify. Repeat this parameter to specify more than one tag, up to a total of 5 tags. The implicit tag all is available to notify all Bindings in a Service instance. Similarly, the implicit tags apn, fcm, gcm, sms and facebook-messenger are available to notify all Bindings in a specific channel.

body
Optional
post string PII MTL: 0 DAYS

The notification text. For FCM and GCM, translates to data.twi_body. For APNS, translates to aps.alert.body. For SMS, translates to body. SMS requires either this body value, or media_urls attribute defined in the sms parameter of the notification.

priority
Optional
post enum:priority Not PII

The priority of the notification. Can be: low or high and the default is high. A value of low optimizes the client app's battery consumption; however, notifications may be delivered with unspecified delay. For FCM and GCM, low priority is the same as Normal priority. For APNS low priority is the same as 5. A value of high sends the notification immediately, and can wake up a sleeping device. For FCM and GCM, high is the same as High priority. For APNS, high is a priority 10. SMS does not support this property.

ttl
Optional
post integer Not PII

How long, in seconds, the notification is valid. Can be an integer between 0 and 2,419,200, which is 4 weeks, the default and the maximum supported time to live (TTL). Delivery should be attempted if the device is offline until the TTL elapses. Zero means that the notification delivery is attempted immediately, only once, and is not stored for future delivery. SMS does not support this property.

title
Optional
post string Not PII

The notification title. For FCM and GCM, this translates to the data.twi_title value. For APNS, this translates to the aps.alert.title value. SMS does not support this property. This field is not visible on iOS phones and tablets but appears on Apple Watch and Android devices.

sound
Optional
post string Not PII

The name of the sound to be played for the notification. For FCM and GCM, this Translates to data.twi_sound. For APNS, this translates to aps.sound. SMS does not support this property.

action
Optional
post string Not PII

The actions to display for the notification. For APNS, translates to the aps.category value. For GCM, translates to the data.twi_action value. For SMS, this parameter is not supported and is omitted from deliveries to those channels.

data
Optional
post object Not PII

The custom key-value pairs of the notification's payload. For FCM and GCM, this value translates to data in the FCM and GCM payloads. FCM and GCM reserve certain keys that cannot be used in those channels. For APNS, attributes of data are inserted into the APNS payload as custom properties outside of the aps dictionary. In all channels, we reserve keys that start with twi_ for future use. Custom keys that start with twi_ are not allowed and are rejected as 400 Bad request with no delivery attempted. For SMS, this parameter is not supported and is omitted from deliveries to those channels.

apn
Optional
post object Not PII

The APNS-specific payload that overrides corresponding attributes in the generic payload for APNS Bindings. This property maps to the APNS Payload item, therefore the aps key must be used to change standard attributes. Adds custom key-value pairs to the root of the dictionary. See the APNS documentation for more details. We reserve keys that start with twi_ for future use. Custom keys that start with twi_ are not allowed.

gcm
Optional
post object Not PII

The GCM-specific payload that overrides corresponding attributes in the generic payload for GCM Bindings. This property maps to the root JSON dictionary. See the GCM documentation for more details. Target parameters to, registration_ids, and notification_key are not allowed. We reserve keys that start with twi_ for future use. Custom keys that start with twi_ are not allowed. GCM also reserves certain keys.

sms
Optional
post object Not PII

The SMS-specific payload that overrides corresponding attributes in the generic payload for SMS Bindings. Each attribute in this value maps to the corresponding form parameter of the Twilio Message resource. These parameters of the Message resource are supported in snake case format: body, media_urls, status_callback, and max_price. The status_callback parameter overrides the corresponding parameter in the messaging service, if configured. The media_urls property expects a JSON array.

facebook_messenger
Optional
post object Not PII

Deprecated.

fcm
Optional
post object Not PII

The FCM-specific payload that overrides corresponding attributes in the generic payload for FCM Bindings. This property maps to the root JSON dictionary. See the FCM documentation for more details. Target parameters to, registration_ids, condition, and notification_key are not allowed in this parameter. We reserve keys that start with twi_ for future use. Custom keys that start with twi_ are not allowed. FCM also reserves certain keys, which cannot be used in that channel.

segment
Optional
post string[] Not PII

The Segment resource is deprecated. Use the tag parameter, instead.

alexa
Optional
post object Not PII

Deprecated.

to_binding
Optional
post string[] Not PII

The destination address specified as a JSON string. Multiple to_binding parameters can be included but the total size of the request entity should not exceed 1MB. This is typically sufficient for 10,000 phone numbers.

Example 1
        
        
        
        
        Send to an existing Binding identified by its Identity

        Send a Notification to existing Bindings (Identity)

        Send to an existing Binding identified by its Identity
        Example 2
              
              
              
              
              Send to an existing Binding identified by its Tag

              Send a Notification to existing Bindings (Tag)

              Send to an existing Binding identified by its Tag
              Example 3
                    
                    
                    
                    
                    Send to existing Bindings identified by their Identity and Tags

                    Send a Notification to existing Bindings (Identity and Tag)

                    Send to existing Bindings identified by their Identity and Tags
                    Example 4
                          
                          
                          
                          
                          Create temporary Bindings in the request using the ToBinding attributes described below.

                          Send a Notification to Bindings in the request

                          Create temporary Bindings in the request using the ToBinding attributes described below.
                          Example 5
                                
                                
                                
                                
                                Configure the notification by channel in the request

                                Send custom notifications by channel

                                Configure the notification by channel in the request

                                ToBinding attributes

                                Name Description
                                binding_type The channel to use. Possible values: sms, apn, gcm, fcm, facebook-messenger.
                                address

                                The destination address.

                                For SMS, it is the phone number in E.164 format.

                                For APNS, GCM and FCM, it is the device or registration token.

                                For Facebook Messenger, it is the Messenger ID.

                                Customize notification by channel

                                Notifications can be tailored to each notification channel to take advantage of channel-specific features or to customize the content to each channel.

                                The channel-specific payload of a notification can be provided by adding a parameter whose name is the BindingType and the value is the channel specific payload in a JSON object. Example 5 includes several such objects.

                                These channels support channel-specific payloads:

                                • APNS
                                • FCM
                                • GCM
                                • SMS
                                • Facebook Messenger

                                The channel-specific content specified in this parameter can add a new property for the channel or replace the value in an existing property, but it cannot remove a property.

                                Example 5 sends a notification to all Bindings with Identity 00000001. A generic Title is specified and will be used for sms binding and specifically for fcm and apn binding types a payload with title and body are also added, which will override the generic Title.

                                Unrecoverable channel errors

                                When the underlying notification channel indicates an unrecoverable error, it is usually because there's a problem with the Binding. To ensure your database contains only valid Bindings, we delete the Bindings that return unrecoverable errors.

                                The following errors are unrecoverable.

                                Channel Error
                                APNS APNS response status code 410 with error string "Unregistered"
                                APNS Address in invalid format
                                GCM / FCM Firebase response status code 200 + error:NotRegistered
                                GCM / FCM Firebase response status code 200 + error:InvalidRegistration
                                GCM / FCM Firebase response status code 200 + error:MismatchSenderId
                                SMS 21604 - 'To' phone number is blank
                                Facebook Messenger (has no unrecoverable errors)
                                Rate this page:

                                Need some help?

                                We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.