.NET
Push Notifications

Push Notifications for .NET

A push notification is a message that is immediately delivered to a offline user's device that is either running your .NET client app in the background or idle. To set a user's connection status to offline, call the SendBirdClient.Disconnect() method. It is recommended that you call the Disconnect() just before your app goes into the background.

Notifications can also be configured to display an alert, play a sound, or badge your app’s icon and are only applicable to group channels. They can't be applied to an open channel due to the massive number of users and messages.

- iOS

Refer to the Push Notification for iOS page to learn more.

- Android

Refer to the Push Notifications for Android page to learn more.


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 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.
        }
    });

    // 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.
        }
    });
});

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.

- iOS

Sendbird server sends notification requests to APNs with the following options:

  • alert: {Sender's nickname}: {Text message}
  • sound: the default alert sound of iOS
  • badge: the total number of each user's unread messages (You can turn off this count from your 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);
}

- Android

A notification message payload consists of two properties: message and sendbird. The message property is a basic string generated according to a chosen notification template in your dashboard under Settings > Application > Notifications > Push notification message templates . The sendbird property is a JSON object which contains all the information about the message a user has sent.

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.
}

Payload

The following is a payload format of the sendbird object, which contains a set of provided key-value items.

Light Color Skin
Copy
{
    "category": "messaging:offline_notification",
    "message": string,           // User input message
    "data": string,              // User custom data
    "mentioned": [],     // 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;
    }
});