When a user logs into your app, you need to programmatically login the user into CometChat.
When a user logs into your app, you need to programmatically login the user into CometChat. But before you log in the user to CometChat, you need to create the user.Summing up-When a user registers in your app
Ideally, user creation should take place at your backend. You can refer our Rest API to learn more about creating a user and use the appropriate code sample based on your backend language.However, if you wish to create users on the fly, you can use the createUser() method. This method takes a User object and the API Key as input parameters and returns the created User object if the request is successful.For more details on the fields present in the User class, please check this
Java
Kotlin
String apiKey = "AUTH_KEY"; // Replace with your API Key.User user = new User();user.setUid("user1"); // Replace with your uid for the user to be created.user.setName("Kevin"); // Replace with the name of the userCometChat.createUser(user, apiKey, new CometChat.CallbackListener<User>() {@Overridepublic void onSuccess(User user) { Log.d("createUser", user.toString());}@Overridepublic void onError(CometChatException e) { Log.e("createUser", e.getMessage());}});
val apiKey = "API_KEY" // Replace with your API Key.val user = User()user.uid = "user1" // Replace with your uid for the user to be created.user.name = "Kevin" // Replace with the name of the userCometChat.createUser(user, apiKey, object : CometChat.CallbackListener<User>() {override fun onSuccess(user: User) { Log.d("createUser", user.toString())}override fun onError(e: CometChatException) { Log.e("createUser", e.message)}})
UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed.
Updating a user similar to creating a user should ideally be achieved at your backend using the Restful APIs. For more information, you can check the update a user section. However, this can be achieved on the fly as well using the updateUser() method. This method takes a User object and the API Key as inputs and returns the updated User object on successful execution of the request.
Java
Kotlin
String apiKey = "AUTH_KEY"; // Replace with your API Key.User user = new User();user.setUid("user1"); // Replace with your uid for the user to be updated.user.setName("Kevin Fernandez"); // Replace with the name of the userCometChat.updateUser(user, apiKey, new CometChat.CallbackListener<User>() {@Overridepublic void onSuccess(User user) { Log.d("updateUser", user.toString());}@Overridepublic void onError(CometChatException e) { Log.e("updateUser", e.getMessage());}});
val apiKey = "API_KEY" // Replace with your API Key.val user = User()user.uid = "user1" // Replace with your uid for the user to be updated.user.name = "Kevin Fernandez" // Replace with the name of the userCometChat.updateUser(user, apiKey, object : CometChat.CallbackListener<User>() {override fun onSuccess(user: User) { Log.d("updateUser", user.toString())}override fun onError(e: CometChatException) { Log.e("updateUser", e.message)}})
Please make sure the User object provided to the updateUser() method has the UID of the user to be updated set.
Updating a logged-in user is similar to updating a user. The only difference being this method does not require an AuthKey. This method takes a User object as input and returns the updated User object on the successful execution of the request.
Java
Kotlin
User user = new User();user.setName("Andrew Joseph");CometChat.updateCurrentUserDetails(user, new CometChat.CallbackListener<User>() {@Overridepublic void onSuccess(User user) { Log.d(TAG, user.toString());}@Overridepublic void onError(CometChatException e) { Log.d(TAG, e.getMessage());}});
val user = User()user.name = "Andrew Joseph"CometChat.updateCurrentUserDetails(user, object : CallbackListener<User>() {override fun onSuccess(user: User) { Log.d(TAG, user.toString())}override fun onError(e: CometChatException) { Log.d(TAG, e.message)}})
By using the updateCurrentUserDetails() method one can only update the logged-in user irrespective of the UID passed. Also, it is not possible to update the role of a logged-in user.