.NET
Push Notifications

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 supported in group channels only, and are not available in open channels.

Android

Please visit our Android Push Notification page for more details.

iOS

Please visit our iOS Push Notification page for more details.


Register or unregister a push token

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

Light Color Skin
Copy
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.

Light Color Skin
Copy
@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

The 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 the badge count in the SendBird Dashboard.)

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

Light Color Skin
Copy
- (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.

Light Color Skin
Copy
{
    "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.

Light Color Skin
Copy
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.

Light Color Skin
Copy
// 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.

Light Color Skin
Copy
// 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;
    }
});