// 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 are only sent for new messages and not for other events. 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 are sent only for new messages.
Only channel members receive push messages.
Members receive push notifications regardless of their online status.
Repliesinside a thread are only sent to users that are part of that thread:- They posted at least one message
They were mentioned
Messages from muted users are not sent.
Messages are sent to all registered devices for a user (up to 25).
Up to 100 members of a channel will receive push notifications for push v1.
Up to 1000 members of a channel will receive push notifications for push v2. This limit can be increased per app through customer support.
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 v2.
Version 1 (legacy) of push notifications won’t be removed but there won’t be any new features. That’s why new applications are highly recommended to use version 2 from the beginning to leverage upcoming new features.
Push Notification Configuration
Firebase
v2: 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: Requires a server key
Firebase console → project settings (top left) → cloud messaging (2th sub header) → add server key
It will generate a long key and save in the firebase server key field in dashboard. A sample key:
AAAAktWCgMU:APA91bGIcyTcdvT0DQqDIfdFPyc5Bk8hH-z5EfFcWI7WFiXOFDKhcspm-Uy_znvEvXfaOOZrFMIP1XAK8drZr7vM70UihXtyz7ZsL3KiSkgkrXk7BJgZZ_VybPLc6ug9baXIAw1zz2nk
For other providers, follow SDK specific docs. Firebase is highlighted here since v1-v2 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.