// you can add this to your `UNUserNotificationCenterDelegate` delegate
// and show a banner notification when a push message is received
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler:
@escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.banner])
}
Overview
Stream Chat supports push notifications through Firebase Cloud Messaging, Apple Push Notification (APN), Huawei Push and Xiaomi Push.
Push notifications can be sent for new messages, message edits and reactions. You can use Webhooks to send push notifications on other types of events.
Setting up Push
Push is available to Stream Chat integrations running in a mobile environment.
Depending on which Stream Chat SDK you are using, the process for setting up push notifications will be slightly different. Follow the guide for your chosen SDK:
Push Delivery Rules
Push message delivery behaves according to these rules:
- Push notifications can be configured for new messages, message edits, message reactions with Push v3.
- Push v2 only supports new messages.
- Only channel members receive a push notification.
- Members receive push notifications regardless of their online status.
- Replies inside a thread are only sent to users that are part of that thread:
- They posted at least one message.
- They were mentioned.
- Push from muted users are not sent.
- Push preferences for a user are respected:
- Preferences at user level are “all”, “none” or “mentions”.
- Preferences at channel level are “all”, “none” or “mentions”.
- Push for a private message are sent only to the restricted users of the message.
- Push notification are sent to all registered devices for a user (up to 25).
- All members of a channel will receive push notifications.
skip_push
is marked asfalse
, as described here.push_notifications
is enabled (default) on the channel type for message is sent.
Push notifications require membership. Watching a channel isn’t enough.
Handling Push Notifications on the Foreground
Both iOS and Android discard push notifications when your application is on the foreground.
You can configure this on your application and decide what to do when a push notification is received while the app is on the foreground.
Push Notification Payload
Push notifications are delivered as data payloads that the SDK can use to convert into the same data types that are received when working with the APIs.
When a message received by the Chat API, according to the delivery rules, it kicks a job that sends a regular data message (as below) to configured push providers on your app. According to the battery and the online status of the device, push providers deliver this payload to the actual devices. When a device receives the payload, it’s passed to the SDK which connects to Chat API to receive regular message and channel records and unmarshals them into in-memory objects and gives control to you by passing these objects. At this point, your application can use these objects to generate any push notification to be shown to the user.
This is the main payload which will be sent to each configured provider:
{
"sender": "stream.chat",
"type": "message.new",
"version": "v2",
"message_id": "d152f6c1-8c8c-476d-bfd6-59c15c20548a",
"id": "d152f6c1-8c8c-476d-bfd6-59c15c20548a",
"channel_type": "messaging",
"channel_id": "company-chat",
"cid": "messaging:company-chat"
}
On both Android and iOS the SDK will convert the payload in channel and message types and allow you to customize the notification message.
You can find more details, examples and guides on specific use-cases on the specific SDK docs.
Make sure to read the “Push Templates” page if your application was created before January 18, 2022, or was not upgraded to push v3.
Push notification versions v1 (legacy) and v2 are deprecated — while they will continue to function, no new features will be added. We strongly recommend that new applications use version 3 from the start to take advantage of upcoming enhancements.
Push Notification Configuration
Firebase
v2 and v3: Requires a service worker account from your FCM project.
Firebase console → project settings (top left) → service accounts (4th sub header) → generate a new private key
Export your key and upload in the firebase credentials field in dashboard. A sample content:
{
"type": "service_account",
"project_id": "chat",
"private_key_id": "5eed79aa9d23954b55a88e96d2eb00d9e72cd662",
"private_key": "-----BEGIN PRIVATE KEY-----\nSOME CONTENT\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-abcdk8@chat.iam.gserviceaccount.com",
"client_id": "106697631437807726111",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-abcdk8%40chat.iam.gserviceaccount.com"
}
- v1: It is deprecated and no longer supports sending push notifications. It is recommended to upgrade to v2 or v3.
For other providers, follow SDK specific docs. Firebase is highlighted here since v2-v3 difference could be confusing.
Troubleshooting
Push notifications are not always intuitive to implement because they involve systems outside of Stream Chat with a number of moving parts. We’ve put together some resources on common errors and how to resolve them.
Other Push Providers
While Stream Chat doesn’t have first class integration for Push providers besides Firebase, APN, Huawei and Xiaomi, it is entirely possible to integrate with additional providers using Webhooks.