SimpleSample-users-android

Ask tech team
From QuickBlox Developers (API docs, code samples, SDK)
Jump to: navigation, search

Contents

Sources

Project homepage on GIT — https://github.com/QuickBlox/quickblox-android-sdk/tree/master/sample-users

Download ZIP - https://github.com/QuickBlox/quickblox-android-sdk/archive/master.zip


Overview

This sample demonstrates how to work with Users QuickBlox module.
It allows to see all users, their details, create user, edit current user, sign in, sign out, use social networks (Facebook, Twitter)

It shows how to:

  1. Retrieve all users
  2. Get users' details
  3. Edit user's parameters
  4. Sign Up, Sign In/Sign out
  5. Sign In with social networks (Facebook, Twitter)



Guide: Get Started with Users API

Get QuickBlox account

http://admin.quickblox.com/register

Create application on Admin panel

http://admin.quickblox.com/apps/new

Also you can look through 5 min guide.

Connect QuickBlox to your application

To get the information on how to connect to the quickblox.jar, please, refer to the Android-how-to-connect-quickblox-jar page.

Add User Base to your application

In order to use QuickBlox Users APIs (any QuickBlox API) you must create session. Then you will have READ access in QuickBlox environment:

Create session

QBAuth.createSession(new QBEntityCallback<QBSession>() {
 
    @Override
    public void onSuccess(QBSession session, Bundle params) {
        // You have successfully created the session
        //
        // Now you can use QuickBlox API!
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Next, you can work with Users API.

Let's register a new user:

Sign Up

Create new User (registration)

Only login/email and password fields are required

final QBUser user = new QBUser("Javck", "javckpassword");
user.setExternalId("45345");
user.setFacebookId("100233453457767");
user.setTwitterId("182334635457");
user.setEmail("Javck@mail.com");
user.setFullName("Javck Bold");
user.setPhone("+18904567812");
StringifyArrayList<String> tags = new StringifyArrayList<String>();
tags.add("car");
tags.add("man");
user.setTags(tags);
user.setWebsite("www.mysite.com");
 
QBUsers.signUp(user, new QBEntityCallback<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

If registration was successful - you can sign in using you login/email and password.

Sign In & Social authorization

There are 4 methods that you can use to sign in to QuickBlox:

  • Sign in with login & password
  • Sign in with email & password
  • Sign in using Facebook/Twitter access token
  • Sign in In with Twitter Digits


Sign In with login & password

QBUser user = new QBUser("garry", "garry2892pass")
 
QBUsers.signIn(user, new QBEntityCallback<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Sign In with email & password

QBUser user = new QBUser();
user.setEmail("garry657@gmail.com");
user.setPassword("garrypass");
 
QBUsers.signIn(user, new QBEntityCallback<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Sign In using Facebook/Twitter access token

String facebookAccessToken = "AAAEra8jNdnkBABYf3ZBSAz9dgLfyK7tQNttIoaZA1cC40niR6HVS0nYuufZB0ZCn66VJcISM8DO2bcbhEahm2nW01ZAZC1YwpZB7rds37xW0wZDZD";
 
QBUsers.signInUsingSocialProvider(QBProvider.FACEBOOK, facebookAccessToken, null, new QBEntityCallback<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Signing In with Twitter Digits

Digits lets people create an account or sign into your app using nothing but their phone number on iOS and Android. Built using the same global, reliable infrastructure Twitter uses, Digits will verify the user’s phone number with a simple customizable user interface that easily integrates into your app.

Read more how to start with Twitter Digits https://docs.fabric.io/android/digits/digits.html

private TwitterAuthConfig authConfig;
 
...
 
initTwitterDigits();
authenticateWithTwitterDigits();
 
...
 
private void initTwitterDigits() {
    if(authConfig == null) {
        // Note: Your consumer key and secret should be obfuscated in your source code before shipping.
        String consumerKey = "A1NXq7BxZ74NZ3dDzXA1HcSN7";
        String consumerSecret = "Piuy52Kf2m2iHVKpfpffi6xjvOYVI904O6sl1c50TLpntTVsl6";
 
        authConfig = new TwitterAuthConfig(consumerKey, consumerSecret);
        Fabric.with(context, new TwitterCore(authConfig), new Digits());
    }
}
 
private void authenticateWithTwitterDigits(final boolean isSync) {
    Digits.authenticate(new AuthCallback() {
        @Override
        public void success(DigitsSession session, String phoneNumber) {
            Map<String, String> authHeaders = getAuthHeadersBySession(session);
 
            String xAuthServiceProvider = authHeaders.get("X-Auth-Service-Provider");
            String xVerifyCredentialsAuthorization = authHeaders.get("X-Verify-Credentials-Authorization");
 
            QBUsers.signInUsingTwitterDigits(xAuthServiceProvider, xVerifyCredentialsAuthorization, new QBEntityCallback<QBUser>() {
                @Override
                public void onSuccess(QBUser user, Bundle params) {
 
                }
 
                @Override
                public void onError(QBResponseException errors) {
 
                }
            });
        }
 
        @Override
        public void failure(DigitsException exception) {
 
        }
    }, "+38");
}
 
private Map<String, String> getAuthHeadersBySession(DigitsSession digitsSession) {
    TwitterAuthToken authToken = (TwitterAuthToken) digitsSession.getAuthToken();
    DigitsOAuthSigning oauthSigning = new DigitsOAuthSigning(authConfig, authToken);
 
    return oauthSigning.getOAuthEchoHeadersForVerifyCredentials();
}

Online\Offline status

Each user has lastRequestAt field - the last user activity time (last query time). You can use it to determine if user online or offline now. For example:

 
long currentTime = System.currentTimeMillis();
long userLastRequestAtTime = user.getLastRequestAt().getTime();
 
// if user didn't do anything last 5 minutes (5*60*1000 milliseconds)    
if((currentTime - userLastRequestAtTime) > 5*60*1000){ 
     // user is offline now
}

Update own profile

If you want to update your profile - just use method bellow. You can update only your own user, just put your User's ID:

Editing User

QBUser user = new QBUser();
user.setId(53779);
user.setFullName("Monro");
user.setEmail("monroJohns987@gmail.com");
 
QBUsers.updateUser(user, new QBEntityCallback<QBUser>(){
    @Override
    public void onSuccess(QBUser user, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Custom data

It's possible to attach custom parameters to the user using customData property:

final QBUser user = new QBUser("monica", "monicapassword");
user.setCustomData("my custom data"); // can contain JSON or any other formats
...

Update profile picture (avatar)

To update user profile picture you have to do:

  1. Upload new picture to Content module
  2. Connect picture to user through user's blobID field
// just create any file
File avatar = ...; 
 
// Upload new avatar to Content module
Boolean fileIsPublic = false;
 
QBContent.uploadFileTask(file1, fileIsPublic, null, new QBEntityCallback<QBFile>() {
    @Override
    public void onSuccess(QBFile qbFile, Bundle params) {
 
        int uploadedFileID = qbFile.getId();
 
        // Connect image to user
        QBUser user = new QBUser();
        user.setId(300);
        user.setFileId(uploadedFileID);
 
        QBUsers.updateUser(user, new QBEntityCallback<QBUser>(){
            @Override
            public void onSuccess(QBUser user, Bundle args) {
 
            }
 
            @Override
            public void onError(QBResponseException errors) {
 
            }
        });
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
}, new QBProgressCallback() {
    @Override
    public void onProgressUpdate(int progress) {
 
    }
});

Now other users can see your avatar.

int userProfilePictureID = user.getFileId(); // user - an instance of QBUser class
 
QBContent.downloadFileById(userProfilePictureID, new QBEntityCallback<InputStream>(){
    @Override
    public void onSuccess(InputStream inputStream, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
}, new QBProgressCallback() {
    @Override
    public void onProgressUpdate(int progress) {
 
    }
});

Reset password

If you forgot your password - you can reset it using method bellow. Email with instruction will be send to your email address.

Reset User's password.

QBUsers.resetPassword("mary987@gmail.com", new QBEntityCallback() {
    @Override
    public void onSuccess() {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Sign Out

If you want to sign out - just use method bellow:

Remove User's session (logout)

QBUsers.signOut(new QBEntityCallback(){
    @Override
    public void onSuccess() {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve users

There are lots of methods to retrieve users:

  • Retrieve all Users
  • Retrieve Users by IDs
  • Retrieve Users by Logins
  • Retrieve Users by Emails
  • Retrieve Users by Phone numbers
  • Retrieve Users by Tags. Tags like groups, in which we can group users
  • Retrieve Users with full name
  • Retrieve User by ID
  • Retrieve User by login
  • Retrieve User by Facebook ID
  • Retrieve User by Twitter ID
  • Retrieve User by email
  • Retrieve User by external ID. You can connect user from 3d party system to QuickBlox user through external ID.


Retrieve all Users

You can use page parameters like:

  • perPage - how many users will contain each page (max 100)
  • page - current page
QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
pagedRequestBuilder.setPage(1);
pagedRequestBuilder.setPerPage(50);
 
QBUsers.getUsers(pagedRequestBuilder, new QBEntityCallback<ArrayList<QBUser>>() {
    @Override
    public void onSuccess(ArrayList<QBUser> users, Bundle params) {
        Log.i(TAG, "Users: " + users.toString());
        Log.i(TAG, "currentPage: " + params.getInt(Consts.CURR_PAGE));
        Log.i(TAG, "perPage: " + params.getInt(Consts.PER_PAGE));
        Log.i(TAG, "totalPages: " + params.getInt(Consts.TOTAL_ENTRIES));
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Sometimes you need to retrieve all your QuickBlox users. But there is one restriction: you can retrieve max 100 users per query.

Here is an example how to retrieve all users:

private void retrieveAllUsersFromPage(int page){
    QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
    pagedRequestBuilder.setPage(page);
    pagedRequestBuilder.setPerPage(100);
 
    QBUsers.getUsers(pagedRequestBuilder, this);
}
 
 
// Start
retrieveAllUsersFromPage(1);
 
static int userNumber = 1;
 
@Override
public void onSuccess(ArrayList<QBUser> users, Bundle params) {
    for(QBUser user : users){
        Log.d("User: ", "N" + userNumber + " is: " + user);
        ++userNumber;
    }
 
    int currentPage = params.getInt(Consts.CURR_PAGE);
    int totalEntries = params.getInt(Consts.TOTAL_ENTRIES);
 
    if(userNumber < totalEntries){
        retrieveAllUsersFromPage(currentPage+1);
    }
}
 
@Override
public void onError(QBResponseException errors) {
 
}

Retrieve Users by IDs

You can use page parameters like:

  • perPage - how many users will contain each page (max 100)
  • page - current returned page
QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
pagedRequestBuilder.setPage(1);
pagedRequestBuilder.setPerPage(50);
 
ArrayList<String> usersIds = new ArrayList<String>();
usersIds.add("378");
usersIds.add("379");
usersIds.add("380");
 
QBUsers.getUsersByIDs(usersIds, pagedRequestBuilder, new QBEntityCallback<ArrayList<QBUser>>() {
    @Override
    public void onSuccess(ArrayList<QBUser> users, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve Users by Logins

You can use page parameters like:

  • perPage - how many users will contain each page (max 100)
  • page - current returned page
QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
pagedRequestBuilder.setPage(1);
pagedRequestBuilder.setPerPage(50);
 
ArrayList<String> usersLogins = new ArrayList<String>();
usersLogins.add("bob");
usersLogins.add("john");
 
QBUsers.getUsersByLogins(usersLogins, pagedRequestBuilder, new QBEntityCallback<ArrayList<QBUser>>() {
    @Override
    public void onSuccess(ArrayList<QBUser> users, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve Users by Emails

You can use page parameters like:

  • perPage - how many users will contain each page (max 100)
  • page - current returned page
QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
pagedRequestBuilder.setPage(1);
pagedRequestBuilder.setPerPage(50);
 
ArrayList<String> usersEmails = new ArrayList<String>();
usersEmails.add("hofnad.amn@gmail.com");
usersEmails.add("sam.cammy@gmail.com");
 
QBUsers.getUsersByEmails(usersEmails, pagedRequestBuilder, new QBEntityCallback<ArrayList<QBUser>>() {
    @Override
    public void onSuccess(ArrayList<QBUser> users, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve Users by Phone numbers

You can use page parameters like:

  • perPage - how many users will contain each page (max 100)
  • page - current returned page
QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
pagedRequestBuilder.setPage(1);
pagedRequestBuilder.setPerPage(50);
 
ArrayList<String> phones = new ArrayList<String>();
phones.add("80999234234");
phones.add("7849293423");
 
QBUsers.getUsersByPhoneNumbers(phones, pagedRequestBuilder, new QBEntityCallback<ArrayList<QBUser>>() {
    @Override
    public void onSuccess(ArrayList<QBUser> users, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve Users by tags

QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
pagedRequestBuilder.setPage(1);
pagedRequestBuilder.setPerPage(50);
 
ArrayList<String> userTags = new ArrayList<String>();
userTags.add("man");
userTags.add("car");
 
QBUsers.getUsersByTags(userTags, pagedRequestBuilder, new QBEntityCallback<ArrayList<QBUser>>() {
    @Override
    public void onSuccess(ArrayList<QBUser> users, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve Users with full name

QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
pagedRequestBuilder.setPage(1);
pagedRequestBuilder.setPerPage(50);
 
QBUsers.getUsersByFullName("garry", pagedRequestBuilder, new QBEntityCallback<ArrayList<QBUser>>() {
    @Override
    public void onSuccess(ArrayList<QBUser> users, Bundle params) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve User by ID

QBUsers.getUser(53779, new QBEntityCallback<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve User by login

QBUsers.getUserByLogin("monica", new QBEntityCallback<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve User by facebook ID

QBUsers.getUserByFacebookId("100003123141430", new QBEntityCallback<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve User by twitter ID

QBUsers.getUserByTwitterId("56802037340", new QBEntityCallback<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve User by email

QBUsers.getUserByEmail("mary32@gmail.com", new QBEntityCallback<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Retrieve User by external ID

QBUsers.getUserByExternalId("123145235", new QBEntityCallback<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle args) {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Delete user

You can delete ownself using methods bellow:

Delete User by ID

QBUsers.deleteUser(564, new QBEntityCallback() {
    @Override
    public void onSuccess() {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Delete User by external ID

QBUsers.deleteByExternalId("568965444", new QBEntityCallback() {
    @Override
    public void onSuccess() {
 
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});

Integrate QuickBlox with your existing User Base

If you want to integrate QuickBlox, but you have already your User Base, you can use externalUserID field. Just set it during QuickBlox user registration:

final QBUser user = new QBUser("Javck", "javckpassword");
user.setExternalId("758902384"); // ID of user from your own user base
 
QBUsers.signUp(user, new QBEntityCallback<QBUser>() { ... })

Next you can retrieve QuickBlox user by external ID by user ID from your user base and use QuickBlox platform features.