Push Notification Configuration

Programmable Chat integrates APN (iOS) and GCM (Android) Push Notifications for certain events. Not all implementations need every possible event to trigger push notifications. Additionally, the content and payload of your push notifications will differ based on requirements and use cases.

Chat Service instances provide some configuration options which allow push configuration on a per Service instance basis. These options allow for:

  • Selecting which of the eligible Chat events should trigger push notifications
  • Specifying the payload template for each message type (overriding the default template)

Note: It is not currently possible to selectively register for various push notification message types on the client SDKs (iOS and Android). This capability is planned and will be delivered in a future release of the relevant SDKs.

Table of Contents

Push Notification Types

The following Push Notifications can be configured for a Chat Service instance:

Push Notification Type Description
New Message This is sent to all members within a Channel when a new Message is posted to the Channel
Added to Channel This is sent to Users that have been added to a Channel
Invited to Channel This is sent to Users that have been invited to join a Channel
Removed from Channel This is sent to Users that have been removed from a Channel they were a Member of

Note: The default enabled flag for new Service instances for all Push Notifications is false. This means that Push will be disabled until you explicitly enable it.

Note: You may configure a sound parameter value for each of the Push Notification types (detailed below).

Push Notification Templates

Each of the Push Notification types has a default template for the payload (or notification body). Each of these templates can be overridden per Service instance via the Push Notification configuration. The templating employs very simple markup for a limited set of variables:

Template Variables

Template Variable Description
${USER} Will be replaced with the FriendlyName of the User who triggered the Push Notification (if any). The User's Identity will be used if no FriendlyName has been set.
${CHANNEL} Will be replaced with the UniqueName, FriendlyName or ChannelSid (if they exist, in that order of priority). These properties are tied to the Channel related to the push notification.
${MESSAGE} Will be replaced with the content of the actual Message. Only used for notifications of type: New Message

Note: The maximum length of the entire notification payload is 110 characters. This substring is applied after the notification payload is constructed and the variables data applied. Thus, freeform text and the variable data are compiled into a string and the first 110 characters are then used as the notification payload.

Note: Variables can be used multiple times within a template, but each variable will contribute to the maximum number of available characters.

Default Templates

Push Notification Type Default Template
New Message ${CHANNEL};${USER}: ${MESSAGE}
Added to Channel You have been added to channel ${CHANNEL} by ${USER}
Invited to Channel ${USER} has invited you to join the channel ${CHANNEL}
Removed from Channel ${USER} has removed you from the channel ${CHANNEL}

Configure Push Notifications

Each Push Notification type can be configured for a Service instance. The configuration allows each notification type to be enabled or disabled. This also handles custom template configuration as per the templating mechanism described above.

The following are the eligible notification type names:

  • NewMessage
  • AddedToChannel
  • InvitedToChannel
  • RemovedFromChannel

The following are the configuration parameters used:

parameter name description
Notifications.[type].Enabled true if this type of push notification should be sent, false if not. Default: false
Notifications.[type].Template The customer template string for the notification type.
Notifications.[type].Sound The "sound" push payload parameter that will be set for this notification type.
Notifications.NewMessage.BadgeCountEnabled true if the NewMessage notification type should send a badge count value in the push payload. This parameter is only applicable to the NewMessage type. Please note that this is currently only used by the iOS APNS push notification type.

Badge Count

This setting is only for the NewMessage notification type. Currently, only APNS push notifications for iOS will use this and include the badge property in the payload. If enabled, the value of this property will represent the count of 1:1 Channels the User is a Member of which have any unread Messages for the User.

If Notifications.NewMessage.BadgeCountEnabled is set to true, decrements to the count of 1:1 Channels with unread messages will be sent to all registered iOS endpoints for that User.

Loading Code Samples...
Language
curl -X POST https://chat.twilio.com/v2/Services/{service sid} \ 
 -d 'Notifications.NewMessage.Enabled=true' \
 -d 'Notifications.NewMessage.Template=A New message in ${CHANNEL} from ${USER}: ${MESSAGE}' \
 -d 'Notifications.NewMessage.Sound=default' \
 -u '{twilio account sid}:{twilio auth token}'
<?php
// This line loads the Twilio PHP Library using Composer
include('vendor/autoload.php');

// Find your Account Sid at https://www.twilio.com/console/account/settings
$TWILIO_ACCOUNT_SID = "YOUR_ACCOUNT_SID";

// Create an API Key and Secret at https://www.twilio.com/console/chat/dev-tools/api-keys
$TWILIO_API_KEY = "YOUR_API_KEY";
$TWILIO_API_SECRET = "YOUR_API_SECRET";

// Your Chat Service SID from https://www.twilio.com/console/chat/services
$CHAT_SERVICE_SID = "YOUR_CHAT_SERVICE_SID";

// Authenticate with Twilio
$client = new Twilio\Rest\Client($TWILIO_API_KEY, $TWILIO_API_SECRET, $TWILIO_ACCOUNT_SID);

// Update the service webhooks
$service = $client->chat->v2->services($CHAT_SERVICE_SID);
$response = $service->update(array(
    "Notifications.NewMessage.Enabled" => "true",
    "Notifications.NewMessage.Template" => 'A New message in ${CHANNEL} from ${USER}: ${MESSAGE}',
    "Notifications.NewMessage.Sound" => "default",
));
Configure the NewMessage Push Notification for a Service instance
Configure NewMessage Push Notifications

Configure the NewMessage Push Notification for a Service instance

Setting additional notification types requires including them in your configuration request. For instance, to include the AddedToChannel Push Notification type, you can add

'Notifications.AddedToChannel.Enabled=true' 
'Notifications.AddedToChannel.Template=You are now a Member of ${CHANNEL}!  Added by ${USER}' 
'Notifications.AddedToChannel.Sound=default'

to your curl request.

Next: Notifications on iOS

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.

1 / 1
Loading Code Samples...
curl -X POST https://chat.twilio.com/v2/Services/{service sid} \ 
 -d 'Notifications.NewMessage.Enabled=true' \
 -d 'Notifications.NewMessage.Template=A New message in ${CHANNEL} from ${USER}: ${MESSAGE}' \
 -d 'Notifications.NewMessage.Sound=default' \
 -u '{twilio account sid}:{twilio auth token}'
<?php
// This line loads the Twilio PHP Library using Composer
include('vendor/autoload.php');

// Find your Account Sid at https://www.twilio.com/console/account/settings
$TWILIO_ACCOUNT_SID = "YOUR_ACCOUNT_SID";

// Create an API Key and Secret at https://www.twilio.com/console/chat/dev-tools/api-keys
$TWILIO_API_KEY = "YOUR_API_KEY";
$TWILIO_API_SECRET = "YOUR_API_SECRET";

// Your Chat Service SID from https://www.twilio.com/console/chat/services
$CHAT_SERVICE_SID = "YOUR_CHAT_SERVICE_SID";

// Authenticate with Twilio
$client = new Twilio\Rest\Client($TWILIO_API_KEY, $TWILIO_API_SECRET, $TWILIO_ACCOUNT_SID);

// Update the service webhooks
$service = $client->chat->v2->services($CHAT_SERVICE_SID);
$response = $service->update(array(
    "Notifications.NewMessage.Enabled" => "true",
    "Notifications.NewMessage.Template" => 'A New message in ${CHANNEL} from ${USER}: ${MESSAGE}',
    "Notifications.NewMessage.Sound" => "default",
));