Skip to content

Update User Properties

Prerequisite

How to Check if the User is Anonymous?

To check if the user is anonymous, use GetSocial.User class:

1
2
3
import im.getsocial.sdk.GetSocial;
// ...
boolean anonymous = GetSocial.User.isAnonymous();

Get User Information

If you want to get user’s name or avatar, you can use relevant getters on GetSocial User:

1
2
3
4
5
// Get User's name:
GetSocial.User.getDisplayName()

// Get User's avatar URL:
GetSocial.User.getAvatarUrl()

Change User Name or Avatar

You can change the display name and/or avatar for the GetSocial user at anytime. Provided display name and avatar will be displayed on activities, comments or any other UI related to the user.

To update display name of the user:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
GetSocial.User.setDisplayName("Happy Tiger", new CompletionCallback() {
    @Override
    public void onSuccess() {
        // GetSocial.User now contains the User with the new name.
        // Update username and any related UI elements.
        Log.d(TAG, "New name is: " + GetSocial.User.getDisplayName());
    }

    @Override
    public void onFailure(GetSocialException e) {
        // Ooops. There was some exception while updating User's details.
        Log.d(TAG, "Name change failed, error: " + e);
    }
});

To update avatar URL of the user:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
GetSocial.User.setAvatarUrl("https://example.com/avatar.png", new CompletionCallback() {
    @Override
    public void onSuccess() {
        // GetSocial.User now contains the User with new avatar.
        // Update avatar and any related UI elements.
        Log.d(TAG, "New avatar is: " + GetSocial.User.getAvatarUrl());
    }

    @Override
    public void onFailure(GetSocialException e) {
        // Ooops. There was some exception while updating User's avatar.
        Log.d(TAG, "AvatarUrl change failed, error: " + e);
    }
});

Also, you can update an avatar using Bitmap object. Recommended resolution is 300x300px. Bigger images will be automatically downscaled on the backend:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Bitmap avatarImage = ...; // get your image here
GetSocial.User.setAvatar(avatarImage, new CompletionCallback() {
    @Override
    public void onSuccess() {
        // GetSocial.User now contains the User with new avatar.
        // Update avatar and any related UI elements.
        Log.d(TAG, "New avatar is: " + GetSocial.User.getAvatar());
    }

    @Override
    public void onFailure(GetSocialException e) {
        // Ooops. There was some exception while updating User's avatar.
        Log.d(TAG, "Avatar change failed, error: " + e);
    }
});

For conveniece, we also provide a method to make those updates together.

To update name and avatar in one batch use:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
UserUpdate userUpdate = UserUpdate.createBuilder()
        .updateDisplayName("Cheerful Tiger")
        .updateAvatarUrl(avatarUrl)
        .build();

GetSocial.User.setUserDetails(userUpdate, new CompletionCallback() {
    @Override
    public void onSuccess() {
        // GetSocial.User now contains the User with new avatar and name.
        // Update avatar and name any related UI elements.
        Log.d(TAG, "New name is: " + GetSocial.User.getDisplayName());
        Log.d(TAG, "New avatar is: " + GetSocial.User.getAvatarUrl());
    }

    @Override
    public void onFailure(GetSocialException e) {
        // Ooops. There was some exception while updating User's details.
        // Developers usually retry or show a dialog with "Please try once again" message.
        Log.d(TAG, "User details change failed, error: " + e);
    }
});

Custom User Properties

There are two kinds of properties: Public and Private. Private properties are visible only for current user. Public properties are visible to other users in PublicUser entity.

Custom properties are String to String map of any information you want to store, located on our servers.

Custom properties have some limitation:

  • Max key length is 32 symbols.
  • Max value length is 1024 symbols.
  • Min value length is 1 symbol, you can not set an empty property, it will just remove a current value if it exists.

This is valid for both Public and Private properties.

Private Properties

To set a private property:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
GetSocial.User.setPrivateProperty(key, value, new CompletionCallback() {
    @Override
    public void onSuccess() {
        Log.d(TAG, "Successfully set a private property");
    }

    @Override
    public void onFailure(GetSocialException exception) {
        Log.d(TAG, "Failed to set a private property, error: " + exception);
    }
});

In case if user already had a value for a current key, it will be overriden. You can prevent it with checking if property already exists:

1
boolean hasPrivateProperty = GetSocial.User.hasPrivateProperty(key);

You can get the current property value:

1
String currentValue = GetSocial.User.getPrivateProperty(key);

If you’re not interesting in keeping property for a user, remove it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
GetSocial.User.removePrivateProperty(key, value, new CompletionCallback() {
    @Override
    public void onSuccess() {
        Log.d(TAG, "Property was successfully removed");
    }

    @Override
    public void onFailure(GetSocialException error) {
        Log.d(TAG, "Failed to remove a property, error: " + error);
    }
});

You can get all properties as unmodifiable copy of origin map:

1
Map<String, String> userProperties = GetSocial.User.getAllPrivateProperties();

Note

You can not modify properties with this map. Also, changes in user properties won’t affect your copy, so you need to refresh it after any user changes. To have up-to-date information, we suggest to use GetSocial SDK methods.

Public Properties

You have similar methods to work with public properties, as for private ones:
setPublicProperty, getPublicProperty, hasPublicProperty and removePublicProperty.

In addition to this, Public properties are visible for other users, so if you have some PublicUser entity, you can get his public properties:

1
String value = user.getPublicProperty(key);

Or, you can check does user have a property:

1
boolean hasProperty = user.hasPublicProperty(key);

You can get all properties as unmodifiable copy of origin map:

1
Map<String, String> userProperties = GetSocial.User.getAllPublicProperties();

Note

You can not modify properties with this map. Also, changes in user properties won’t affect your copy, so you need to refresh it after any user changes. To have up-to-date information, we suggest to use GetSocial SDK methods.

Batch Properties Update

If you want to perform multiple updates of user data, you can do it with UserUpdate class. It allows you update few user properties(custom and not) in one request:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
UserUpdate batchUpdate = UserUpdate.createBuilder()
        .updateAvatarUrl(newAvatarUrl)
        .updateDisplayName(newDisplayName)
        .setPublicProperty(publicProperty1, newPublicValue)
        .removePublicProperty(publicProperty2)
        .setPrivateProperty(privateProperty1, newPrivateValue)
        .removePrivateProperty(privateProperty2)
        .build();

GetSocial.User.setUserDetails(batchUpdate, new CompletionCallback() {
    @Override
    public void onSuccess() {
        Log.d(TAG, "User details were successfully updated");
    }

    @Override
    public void onFailure(GetSocialException exception) {
        Log.d(TAG, "Failed to update user details, error: " + exception);
    }
});

Subscribe to User Lifecycle Changes

Implement im.getsocial.sdk.usermanagement.OnUserChangedListener interface in your activity:

1
public class MainActivity extends Activity implements OnUserChangedListener {

Register for the updates in Activity onCreate method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Setup GetSocial
    GetSocial.User.setOnUserChangedListener(this);

    ...
}

Implement onUserChanged() method

1
2
3
4
5
6
7
8
@Override
public void onUserChanged() {
    Log.d(TAG, "Is SDK initialized?        " + GetSocial.isInitialized());
    Log.d(TAG, "Is user anonymous?         " + GetSocial.User.isAnonymous());
    Log.d(TAG, "What's user's avatar?      " + GetSocial.User.getAvatarUrl());
    Log.d(TAG, "What's user's name?        " + GetSocial.User.getDisplayName());
    Log.d(TAG, "List of user's identities: " + GetSocial.User.getAuthIdentities());
}

Give us your feedback! Was this article helpful?

😀 🙁