Push Notifications

You can set up Push Notifications so that users can receive messages even when they are offline. To set a user's connection status to offline, call SendBirdClient.Disconnect(). It is recommended that you call Disconnect() just before your app goes into the background.

Note: Push notifications are only supported in Group Channel. The SDK does not provide an option for users to receive push notifications from Open Channel.

Android

Please visit our Android Push Notification page for more details.

iOS

Please visit our iOS Push Notification page for more details.


Registering or unregistering a push token

Call SendBirdClient.Connect() to complete the token registration process, which is currently in a pending state.

SendBirdClient.Connect(userId, (User user, SendBirdException e) => {
    if (e != null) {
        // Error.
        return;
    }

    if (SendBirdClient.GetPendingPushToken() == null) return;

    // For Android
    SendBirdClient.RegisterFCMPushTokenForCurrentUser(SendBirdClient.GetPendingPushToken(), (SendBirdClient.PushTokenRegistrationStatus status, SendBirdException e1) => {
        if (e1 != null) {
            // Error.
            return;
        }

    if (status == SendBirdClient.PushTokenRegistrationStatus.PENDING) {
            // Try registration after connection is established.
        }    

    });

    // For iOS
    SendBirdClient.RegisterAPNSPushTokenForCurrentUser(SendBirdClient.GetPendingPushToken(), (SendBirdClient.PushTokenRegistrationStatus status, SendBirdException e1) => {
        if (e1 != null) {
            // Error.
            return;
        }

    if (status == SendBirdClient.PushTokenRegistrationStatus.PENDING) {
            // Try registration after connection is established.
        }

    });
});

Offline users now receives Push notifications which each contain information about the message. Parse these to display the messages for a user. SendBird also sends an additional payload with a sendbird key.

Android

The message property (for example, "Sender: Message") is a basic string. The payload property is a JSON string with full information on the request.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    String message = remoteMessage.getData().get("message");
    JsonElement payload = new JsonParser().parse(remoteMessage.getData().get("sendbird"));
    sendNotification(message, payload);
}

private void sendNotification(String message, JsonElement payload) {  
  // Your own way to show notifications to users.
}

iOS

SendBird APNS Push notifications are sent with the following options.

  • alert: "{Sender Nickname}: {Text Message}"
  • sound: default
  • badge: total unread message count of each user (You can disable "badge" count in SendBird Dashboard)

You could parse the payload in "didReceiveRemoteNotification" and use it to handle user reactions.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler{

    NSString *alertMsg = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    NSDictionary *payload = [userInfo objectForKey:@"sendbird"];
      // Your custom way to parse data
    completionHandler(UIBackgroundFetchResultNewData);
}

Payload

Here is a format of each payload.

{
  "category": "messaging:offline_notification",
  "message": string,           // User input message
  "data": string,              // User custom data
  "mentioned": [string, ],     // Mentioned User IDs
  "unread_message_count": int, // Total unread message count of messaging channels
  "channel": {
    "channel_url": string,     // Group channel URL
    "name": string,            // Group channel name 
  },
  "channel_type": string,      // messaging, group_messaging, chat
  "sender": {
    "id": string,              // Sender's unique ID
    "name": string,            // Sender's nickname
  },
  "recipient": {
    "id": string,              // Recipient's unique ID
    "name": string,            // Recipient's nickname
  },
  files: []  // If a message is a file link, this array represents files
}

Notification Preference

If you need to turn push notification off, it can be done by removing all tokens from users.

public void DisablePushNotification() {
    // Android
    // If you want to unregister the current device only, invoke this method.
    SendBirdClient.UnregisterFCMPushTokenForCurrentUser(fcmRegToken, (SendBirdException e) => {
        if (e != null) {
            // Error.
            return;
        }
    });

    // iOS
    // If you want to unregister the current device only, invoke this method.
    SendBirdClient.UnregisterAPNSPushTokenForCurrentUser(apnsRegToken, (SendBirdException e) => {
        if (e != null) {
            // Error.
            return;
        }
    });

    // If you want to unregister the all devices of the user, invoke this method.
    SendBirdClient.UnregisterPushTokenAllForCurrentUser((SendBirdException e) => {
        if (e != null) {
            // Error.
            return;
        }
    });
}

You can also set push notification on/off setting for a specific group channel.

// If you want to turn push notification for this channel on, set this true.
groupChannel.SetPushPreference(TRUE_OR_FALSE, (SendBirdException e) => {
    if (e != null) {
        // Error.
        return;
    }
});

If you want to snooze alarms (notifications) for some periods, SetDoNotDisturb is here for you.

// The current logged-in user doesn't receive push notifications during the specified time.
SendBirdClient.SetDoNotDisturb(TRUE_OR_FALSE, START_HOUR, START_MIN, END_HOUR, END_MIN, "America/Los_Angeles", (SendBirdException e) => {
    if (e != null) {
        // Error.
        return;
    }
});