Push Templates

All apps using push v2 or created after January 18, 2022 do not need to configure any template in general. However, some platforms might still need to add templates. This is a general description of templating. For templating in v2, see details here. For migrating to v2, see details here.

For both Firebase and APN, the payload that is being sent is rendered using the handlebars templating language, to ensure full configurability for your app.

Stream provides the following variables in the template rendering context:

Context Variables

NameTypeDescription
channelobjectChannel object. You can access the channel name and any other custom field you have defined for this channel
senderobjectSender object. You can access the user name, id or any other custom field you have defined for the user
receiverobjectReceiver object. You can access the user name, id or any other custom field you have defined for the user
messageobjectMessage object. You can access the text of the message (or a preview of it if the message is too large) or any other custom field you have defined for the message
membersarrayChannel members. You can access the user name, id and any other custom field of each member (i.e. excluding sender)
otherMembersarrayLike members but the user who will be receiving the notification is excluded (i.e. excluding sender and receiver)
unread_countintegerNumber of unread messages
unread_channelsintegerNumber of unread channels for this user

members andotherMembers aren’t available in the v2 template context.

Defaults

v1

When editing APN/Firebase settings, if you leave the notification_template or data_template field empty, default templates will be used.

v2

Editing data template is forbidden. There is no default for notification_template, it’s sent only if set. apn_template is introduced for apn templating under firebase.

APN default (v1):

{
  "aps" : {
    "alert": {
      "title": "{{ sender.name }} @ {{ channel.name }}",
      "body": "{{ truncate message.text 2000 }}"
    },
    "badge": {{ unread_count }},
    "category": "NEW_MESSAGE"
  },
  "stream": {
    "sender": "stream.chat",
    "type": "message.new",
    "version": "v1",
    "id": "{{ message.id }}",
    "cid": "{{ channel.cid }}"
  }
}

Firebase default notification template (v1):

{
  "title": "{{ sender.name }} @ {{ channel.name }}",
  "body": "{{ truncate message.text 2000 }}",
  "click_action": "OPEN_ACTIVITY_1",
  "sound": "default"
}

Firebase default data template (v1):

{
  "sender": "{{ sender.id }}",
  "channel": {
    "type": "{{ channel.type }}",
    "id": "{{ channel.id }}"
  },
  "message": "{{ message.id }}"
}

APN default and Firebase default APN template (v2):

{
  "aps" : {
    "alert": {
      "title": "New message from {{ sender.name }}",
      "body": "{{ truncate message.text 2000 }}"
    },
    "mutable-content": 1,
    "category": "stream.chat"
  },
  "stream": {
    "sender": "stream.chat",
    "type": "message.new",
    "version": "v2",
    "id": "{{ message.id }}",
    "cid": "{{ channel.cid }}"
  }
}

Generic default data payload (v2):

{
 "sender": "stream.chat",
 "type": "message.new",
 "version": "v2",
 "message_id": "{{ message.id }}",
 "id": "{{ message.id }}",
 "channel_type": "{{ channel.type }}",
 "channel_id": "{{ channel.id }}",
 "cid": "{{ channel_cid }}"
}

Limitations

There are some limitations that Stream imposes on the push notification handlebars template to make sure no malformed payloads are being sent to push providers.

1: Custom Arrays Can’t Be Indexed

For example, given the context:

{
  "sender":{
    "name": "Bob",
    "some_array": ["foo","bar"]
  }
}

And the template:

"title": {{ sender.some_array.[0] }}

The rendered payload will be:

"title": ""

2: Interpolating Whole Lists and Objects Isn’t Allowed

For example, given the context:

{
  "sender":{
    "name": "bob",
    "some_array": ["foo","bar"],
    "address": {
      "street": "willow str"
    }
  }
}

And the template:

"title": "{{ sender.some_array }} {{ sender.address }}"

The rendered payload will be:

"title": "[] {}"

3: Unquoted fields that aren’t in the context will be rendered as empty strings

For example, given the context:

{
  "sender":{
    "name": "bob"
  }
}

And the template:

"title": {{ sender.missing_field }}

The rendered payload will be:

"title": ""

Advanced Use Cases

For advanced use cases (e.g. A list of channel members in the notification title, conditional rendering, etc), Stream provides some handlebars helper functions.

Helper Functions

nametypedescription
implodemembersfunctiontakes the list of channel members and implodes it into a single string, using a custom limit, separator and suffix.
jsonfunctionrenders passed parameter as JSON (e.g {“channel”:{{{ json channel }}}} )
eachfunctionFor loop. Use this to access the current variable, @index for the current index and @first and @last as convenience booleans to determine if the iteration is at its first/last element
iffunctionIf function. Tests trueness of given parameter. Supports else statement. (e.g {{#if sender.name}}{{ sender.name }}{{/if}} )
unlessfunctionUnless function. Tests falseness of given parameter. Supports else statement. (e.g {{#unless sender.name}}Missing name{{/unless}} )
equalfunctionEquality check function. Tests equality of the given 2 parameters. Supports else statement. (e.g {{#equal channel.type “messaging” }}This is the messaging channel{{else}}This is another channel{{/equal}} )
unequalfunctionInequality check function. Tests inequality of the given 2 parameters. Supports else statement. (e.g {{#unequal channel.type “messaging” }}This is another channel{{else}}This is the messaging channel{{/unequal}} )
ifLtfunctionIf less than. Supports else statement.
ifLtefunctionIf less than or equal. Supports else statement.
ifGtfunctionIf greater than. Supports else statement.
ifGtefunctionIf greater than or equal. Supports else statement.
remainderfunctionCalculates the difference between the length of an array and an integer (e.g {{remainder otherMembers 2}}
truncatefunctionTruncate given text to given length (e.g {{ truncate message.text 2000 }})

Most of the functions above are straight forward, except for implodeMembers, which will be detailed further.

The full function signature is: {{implodeMembers otherMembers|members [limit=] [separator=] [nameField=] [suffixFmt=]}}

Function Parameters

nametypedescriptiondefault
otherMembersmembersarrayWhich member array to implode
limitintegerHow many member names to show before adding the suffix3
nameFieldstringField name from which field to retrieve the member’s name. Note: does not support nestingname
separatorstringSeparator to use for channel members,
suffixFmtstringFormat string to use for the suffix. Note: only %d is allowed for formattingand %d other(s)

Examples

Let’s put these helpers to use in a few examples:

Example 1

Rendering channel members in the notification title. Each member’s name is stored in the fullName field.

What we want to achieve:

{
  "aps": {
    "alert": {
      "title": "Bob Jones, Jessica Wright, Tom Hadle and 4 other(s)",
      "body": "Bob Jones: Hello there fellow channel members"
    },
    "badge": 0
  }
}

How we will achieve it: using implodeMembers with a custom name field (leaving others empty so that defaults will be used):

{
  "aps": {
    "alert": {
      "title": "{{implodeMembers otherMembers nameField="fullName"}}",
      "body": "{{ sender.fullName }}: {{ message.text }}"
    },
    "badge": {{ unread_count }}
  }
}

Example 2

Rendering channel members in the notification title. Each member’s name is stored in the nested details.name field.

What we want to achieve:

{
  "aps": {
    "alert": {
      "title": "Bob Jones, Jessica Wright, Tom Hadle and 4 other(s)",
      "body": "Bob Jones: Hello there fellow channel members"
    },
    "badge": 0
  }
}

How we will achieve it: since implodeMembers doesn’t support nested fields, we need to use a bunch of helpers such as each, ifLte. Note how the use of ~ will trim the whitespaces so that the title in rendered in a single row:

{
  "aps": {
    "alert": {
      "title": "
      {{~#each otherMembers}}
        {{#ifLte @index 2}}
          {{~this.details.name}}{{#ifLt @index 2 }}, {{/ifLt~}}
        {{~else if @last~}}
          {{{ " " }}} and {{remainder otherMembers 3}} other(s)
        {{~/ifLte~}}
      {{/each~}}",
      "body": "{{ sender.details.name }}: {{ message.text }}"
    },
    "badge": {{ unread_count }}
  }
© Getstream.io, Inc. All Rights Reserved.