Android SDK
(moved Android SDK to Android SDK 2.2.0) |
(→Social Login) |
||
(3 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
− | + | = Download and Setup the SDK = | |
+ | |||
+ | You can download and setup the SDK using our [[Android SDK Setup]] guide. | ||
+ | |||
+ | = Social Login = | ||
+ | |||
+ | Add the following statements to your Activity: | ||
+ | |||
+ | import com.coffeebeantech.socialidsdk.SocialId; | ||
+ | |||
+ | Configure all providers you'll support. This can be performed in your Activity's ''onCreate()'' method. | ||
+ | |||
+ | import com.coffeebeantech.socialidsdk.sociallogin.providers.EmailConfiguration; | ||
+ | import com.coffeebeantech.socialidsdk.sociallogin.providers.FacebookConfiguration; | ||
+ | import com.coffeebeantech.socialidsdk.sociallogin.providers.GplusConfiguration; | ||
+ | import com.coffeebeantech.socialidsdk.sociallogin.providers.LinkedinConfiguration; | ||
+ | import com.coffeebeantech.socialidsdk.sociallogin.providers.TwitterConfiguration; | ||
+ | |||
+ | // Facebook configuration | ||
+ | // Extra permissions are optional | ||
+ | FacebookConfiguration facebookConfiguration = new FacebookConfiguration() | ||
+ | .setPermissions(Arrays.asList("public_profile", "user_birthday", "email", "user_location")); | ||
+ | |||
+ | // Google+ configuration | ||
+ | GplusConfiguration gplusConfiguration = new GplusConfiguration() | ||
+ | .setClientServerId(WEB_APPLICATION_CLIENT_ID); | ||
+ | |||
+ | // LinkedIn configuration | ||
+ | LinkedinConfiguration linkedinConfiguration = new LinkedinConfiguration() | ||
+ | .setCallbackUri(LINKEDIN_CALLBACK_URI); | ||
+ | |||
+ | // Twitter configuration | ||
+ | // Key and Secret are mandatory | ||
+ | TwitterConfiguration twitterConfiguration = new TwitterConfiguration() | ||
+ | .setTwitterKey(TWITTER_KEY) | ||
+ | .setTwitterSecret(TWITTER_SECRET); | ||
+ | |||
+ | // Email configuration | ||
+ | EmailConfiguration emailConfiguration = new EmailConfiguration(); | ||
+ | |||
+ | SocialId.configLoginProviders(facebookConfiguration, gplusConfiguration, linkedinConfiguration, twitterConfiguration, emailConfiguration); | ||
+ | |||
+ | After provider configuration, add the following code to finish the login configuration: | ||
+ | |||
+ | SocialId.configLogin(this, new LoginEventListenerImpl(this)); | ||
+ | |||
+ | If you want to pass additional configuration, such as the default user profile fields to get from the Social-ID platform, you can use the following code instead. See [[Social Profile Fields]] for the available profile fields. | ||
+ | |||
+ | import com.coffeebeantech.socialidsdk.sociallogin.SocialLoginConfiguration; | ||
+ | |||
+ | SocialLoginConfiguration socialLoginConfiguration = new SocialLoginConfiguration(). | ||
+ | setUserFields(Arrays.asList("display_name", "picture_url", "current_location", "verified_email", "gender")); | ||
+ | |||
+ | SocialId.configLogin(this, new LoginEventListenerImpl(this), socialLoginConfiguration); | ||
+ | |||
+ | Add the following code to the end of the Activity's ''onActivityResult()'' method: | ||
+ | |||
+ | SocialId.onActivityResult(requestCode, resultCode, data); | ||
+ | |||
+ | Add the following code to the end of the Activity's ''onRequestPermissionsResult()'' method: | ||
+ | |||
+ | SocialId.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||
+ | |||
+ | Call the following code to start the login process: | ||
+ | |||
+ | import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; | ||
+ | import com.coffeebeantech.socialidsdk.util.Provider; | ||
+ | |||
+ | SocialId.initLogin(Provider.FACEBOOK); // you can change FACEBOOK to LINKEDIN, TWITTER, GPLUS or EMAIL | ||
+ | |||
+ | For Email login, you should pass the required parameters through the EmailLoginParameters parameter: | ||
+ | |||
+ | import com.coffeebeantech.socialidsdk.sociallogin.providers.EmailLoginParameters; | ||
+ | |||
+ | EmailLoginParameters emailLoginParameters = new EmailLoginParameters(); | ||
+ | emailLoginParameters.setPassword(password); | ||
+ | emailLoginParameters.setEmailAddress(email); | ||
+ | |||
+ | SocialId.initLogin(Provider.EMAIL, emailLoginParameters); | ||
+ | |||
+ | Finally, create a LoginEventListenerImpl class using this example: | ||
+ | |||
+ | import com.coffeebeantech.socialidapi.models.marketing.login.User; | ||
+ | import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; | ||
+ | import com.coffeebeantech.socialidsdk.sociallogin.LoginEventListener; | ||
+ | import com.coffeebeantech.socialidsdk.util.Provider; | ||
+ | |||
+ | public class LoginEventListenerImpl implements LoginEventListener { | ||
+ | |||
+ | private Activity mActivity; | ||
+ | |||
+ | public LoginEventListenerImpl(Activity activity) { | ||
+ | this.mActivity = activity; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onBack() { | ||
+ | // onBack code | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onCancel() { | ||
+ | // onCancel code | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onComplete(Provider provider, User user) { | ||
+ | // onComplete code | ||
+ | // PersonProfile personProfile = user.getProfile(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onError(SocialIdException e) { | ||
+ | // onError code | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onPreExecute() { | ||
+ | // onPreExecute code | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onPostExecute() { | ||
+ | // onPostExecute code | ||
+ | } | ||
+ | } | ||
+ | |||
+ | = Logout = | ||
+ | |||
+ | Use the following code to log out a user: | ||
+ | |||
+ | import com.coffeebeantech.socialidsdk.SocialId; | ||
+ | |||
+ | SocialId.logoutUser(); | ||
+ | |||
+ | = Get the logged user = | ||
+ | |||
+ | Use the following code to get the current logged user in cache: | ||
+ | |||
+ | import com.coffeebeantech.socialidapi.models.marketing.database.PersonProfile; | ||
+ | import com.coffeebeantech.socialidapi.models.marketing.login.User; | ||
+ | import com.coffeebeantech.socialidsdk.SocialId; | ||
+ | |||
+ | User user = SocialId.getCurrentUser(); | ||
+ | PersonProfile personProfile = user.getProfile(); | ||
+ | // you can now call personProfile.getDisplayName(), personProfile.getPictureUrl() and other methods to get profile data | ||
+ | |||
+ | If you want a refreshed user from the Social Id platform, you can use the following code instead: | ||
+ | |||
+ | import com.coffeebeantech.socialidapi.models.marketing.login.User; | ||
+ | import com.coffeebeantech.socialidsdk.SocialId; | ||
+ | import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; | ||
+ | import com.coffeebeantech.socialidsdk.util.SocialIdCallback; | ||
+ | |||
+ | SocialId.getCurrentUser(new SocialIdCallback<User>() { | ||
+ | @Override | ||
+ | public void onSuccess(User user) { | ||
+ | // PersonProfile personProfile = user.getProfile(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onError(SocialIdException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | = User Push Notifications = | ||
+ | |||
+ | In order to send push notifications directly to a specific user, you can link his device with his account. You can make this process whenever you want after the user logs in: | ||
+ | |||
+ | import com.coffeebeantech.socialidsdk.SocialId; | ||
+ | import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; | ||
+ | import com.coffeebeantech.socialidsdk.util.SocialIdCallback; | ||
+ | |||
+ | if(SocialId.getDeviceId() != null && !SocialId.getDeviceId().isEmpty()) { | ||
+ | SocialId.linkDeviceToUser(new SocialIdCallback<Void>() { | ||
+ | @Override | ||
+ | public void onSuccess(Void v) { | ||
+ | // onSuccess code | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onError(SocialIdException e) { | ||
+ | // onError code | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | |||
+ | = Create user with email and password = | ||
+ | |||
+ | You can register a user by following the example below: | ||
+ | |||
+ | import com.coffeebeantech.socialidapi.models.marketing.login.User; | ||
+ | import com.coffeebeantech.socialidapi.models.marketing.login.UserAttributes; | ||
+ | import com.coffeebeantech.socialidapi.models.marketing.login.UserDataAttributes; | ||
+ | import com.coffeebeantech.socialidsdk.SocialId; | ||
+ | import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; | ||
+ | import com.coffeebeantech.socialidsdk.util.SocialIdCallback; | ||
+ | |||
+ | // user attributes - required | ||
+ | UserAttributes userAttributes = new UserAttributes(); | ||
+ | // user data attributes - optional | ||
+ | UserDataAttributes userDataAttributes = new UserDataAttributes(); | ||
+ | Map<String, Object> userDataFields = new HashMap<String, Object>(); | ||
+ | |||
+ | userAttributes.setEmailAddress(emailAddress); // or setUsername(username) | ||
+ | userAttributes.setPassword(password); | ||
+ | userDataFields.put("name", name); | ||
+ | userDataFields.put("birthday", birthday); | ||
+ | userDataAttributes.setFields(userDataFields); | ||
+ | |||
+ | SocialId.createUser(userAttributes, userDataAttributes, new SocialIdCallback<User>() { | ||
+ | @Override | ||
+ | public void onSuccess(User user) { | ||
+ | // user was created and is already logged in | ||
+ | // PersonProfile personProfile = user.getProfile(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onError(SocialIdException e) { | ||
+ | // onError code | ||
+ | }); | ||
+ | } | ||
+ | |||
+ | You can also provide a reset password flow for users with the following code: | ||
+ | |||
+ | import com.coffeebeantech.socialidapi.models.marketing.login.ResetPasswordEmailAttributes; | ||
+ | import com.coffeebeantech.socialidapi.util.Language; | ||
+ | import com.coffeebeantech.socialidsdk.SocialId; | ||
+ | import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; | ||
+ | import com.coffeebeantech.socialidsdk.util.SocialIdCallback; | ||
+ | |||
+ | ResetPasswordEmailAttributes resetPasswordEmailAttributes = new ResetPasswordEmailAttributes(); | ||
+ | |||
+ | resetPasswordEmailAttributes | ||
+ | .setEmailAddress(emailAddress) // or setUsername(username) | ||
+ | .setRedirectUrl(redirectUrl) // required | ||
+ | .setSubject(subject) // optional | ||
+ | .setFrom(from) // optional | ||
+ | .setDescription(description) // optional | ||
+ | .setLogoUrl(logoUrl) // optional | ||
+ | .setLanguage(Language.ENGLISH); // optional | ||
+ | |||
+ | SocialId.resetUserPassword(resetPasswordEmailAttributes, new SocialIdCallback<Void>() { | ||
+ | @Override | ||
+ | public void onSuccess(Void v) { | ||
+ | // onSuccess code | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onError(SocialIdException e) { | ||
+ | // onError code | ||
+ | }); | ||
+ | } |
Latest revision as of 14:15, 27 February 2019
Contents |
Download and Setup the SDK
You can download and setup the SDK using our Android SDK Setup guide.
Social Login
Add the following statements to your Activity:
import com.coffeebeantech.socialidsdk.SocialId;
Configure all providers you'll support. This can be performed in your Activity's onCreate() method.
import com.coffeebeantech.socialidsdk.sociallogin.providers.EmailConfiguration; import com.coffeebeantech.socialidsdk.sociallogin.providers.FacebookConfiguration; import com.coffeebeantech.socialidsdk.sociallogin.providers.GplusConfiguration; import com.coffeebeantech.socialidsdk.sociallogin.providers.LinkedinConfiguration; import com.coffeebeantech.socialidsdk.sociallogin.providers.TwitterConfiguration; // Facebook configuration // Extra permissions are optional FacebookConfiguration facebookConfiguration = new FacebookConfiguration() .setPermissions(Arrays.asList("public_profile", "user_birthday", "email", "user_location")); // Google+ configuration GplusConfiguration gplusConfiguration = new GplusConfiguration() .setClientServerId(WEB_APPLICATION_CLIENT_ID); // LinkedIn configuration LinkedinConfiguration linkedinConfiguration = new LinkedinConfiguration() .setCallbackUri(LINKEDIN_CALLBACK_URI); // Twitter configuration // Key and Secret are mandatory TwitterConfiguration twitterConfiguration = new TwitterConfiguration() .setTwitterKey(TWITTER_KEY) .setTwitterSecret(TWITTER_SECRET); // Email configuration EmailConfiguration emailConfiguration = new EmailConfiguration(); SocialId.configLoginProviders(facebookConfiguration, gplusConfiguration, linkedinConfiguration, twitterConfiguration, emailConfiguration);
After provider configuration, add the following code to finish the login configuration:
SocialId.configLogin(this, new LoginEventListenerImpl(this));
If you want to pass additional configuration, such as the default user profile fields to get from the Social-ID platform, you can use the following code instead. See Social Profile Fields for the available profile fields.
import com.coffeebeantech.socialidsdk.sociallogin.SocialLoginConfiguration; SocialLoginConfiguration socialLoginConfiguration = new SocialLoginConfiguration(). setUserFields(Arrays.asList("display_name", "picture_url", "current_location", "verified_email", "gender")); SocialId.configLogin(this, new LoginEventListenerImpl(this), socialLoginConfiguration);
Add the following code to the end of the Activity's onActivityResult() method:
SocialId.onActivityResult(requestCode, resultCode, data);
Add the following code to the end of the Activity's onRequestPermissionsResult() method:
SocialId.onRequestPermissionsResult(requestCode, permissions, grantResults);
Call the following code to start the login process:
import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; import com.coffeebeantech.socialidsdk.util.Provider; SocialId.initLogin(Provider.FACEBOOK); // you can change FACEBOOK to LINKEDIN, TWITTER, GPLUS or EMAIL
For Email login, you should pass the required parameters through the EmailLoginParameters parameter:
import com.coffeebeantech.socialidsdk.sociallogin.providers.EmailLoginParameters; EmailLoginParameters emailLoginParameters = new EmailLoginParameters(); emailLoginParameters.setPassword(password); emailLoginParameters.setEmailAddress(email); SocialId.initLogin(Provider.EMAIL, emailLoginParameters);
Finally, create a LoginEventListenerImpl class using this example:
import com.coffeebeantech.socialidapi.models.marketing.login.User; import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; import com.coffeebeantech.socialidsdk.sociallogin.LoginEventListener; import com.coffeebeantech.socialidsdk.util.Provider; public class LoginEventListenerImpl implements LoginEventListener { private Activity mActivity; public LoginEventListenerImpl(Activity activity) { this.mActivity = activity; } @Override public void onBack() { // onBack code } @Override public void onCancel() { // onCancel code } @Override public void onComplete(Provider provider, User user) { // onComplete code // PersonProfile personProfile = user.getProfile(); } @Override public void onError(SocialIdException e) { // onError code e.printStackTrace(); } @Override public void onPreExecute() { // onPreExecute code } @Override public void onPostExecute() { // onPostExecute code } }
Logout
Use the following code to log out a user:
import com.coffeebeantech.socialidsdk.SocialId; SocialId.logoutUser();
Get the logged user
Use the following code to get the current logged user in cache:
import com.coffeebeantech.socialidapi.models.marketing.database.PersonProfile; import com.coffeebeantech.socialidapi.models.marketing.login.User; import com.coffeebeantech.socialidsdk.SocialId; User user = SocialId.getCurrentUser(); PersonProfile personProfile = user.getProfile(); // you can now call personProfile.getDisplayName(), personProfile.getPictureUrl() and other methods to get profile data
If you want a refreshed user from the Social Id platform, you can use the following code instead:
import com.coffeebeantech.socialidapi.models.marketing.login.User; import com.coffeebeantech.socialidsdk.SocialId; import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; import com.coffeebeantech.socialidsdk.util.SocialIdCallback; SocialId.getCurrentUser(new SocialIdCallback<User>() { @Override public void onSuccess(User user) { // PersonProfile personProfile = user.getProfile(); } @Override public void onError(SocialIdException e) { e.printStackTrace(); } });
User Push Notifications
In order to send push notifications directly to a specific user, you can link his device with his account. You can make this process whenever you want after the user logs in:
import com.coffeebeantech.socialidsdk.SocialId; import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; import com.coffeebeantech.socialidsdk.util.SocialIdCallback; if(SocialId.getDeviceId() != null && !SocialId.getDeviceId().isEmpty()) { SocialId.linkDeviceToUser(new SocialIdCallback<Void>() { @Override public void onSuccess(Void v) { // onSuccess code } @Override public void onError(SocialIdException e) { // onError code } }); }
Create user with email and password
You can register a user by following the example below:
import com.coffeebeantech.socialidapi.models.marketing.login.User; import com.coffeebeantech.socialidapi.models.marketing.login.UserAttributes; import com.coffeebeantech.socialidapi.models.marketing.login.UserDataAttributes; import com.coffeebeantech.socialidsdk.SocialId; import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; import com.coffeebeantech.socialidsdk.util.SocialIdCallback; // user attributes - required UserAttributes userAttributes = new UserAttributes(); // user data attributes - optional UserDataAttributes userDataAttributes = new UserDataAttributes(); Map<String, Object> userDataFields = new HashMap<String, Object>(); userAttributes.setEmailAddress(emailAddress); // or setUsername(username) userAttributes.setPassword(password); userDataFields.put("name", name); userDataFields.put("birthday", birthday); userDataAttributes.setFields(userDataFields); SocialId.createUser(userAttributes, userDataAttributes, new SocialIdCallback<User>() { @Override public void onSuccess(User user) { // user was created and is already logged in // PersonProfile personProfile = user.getProfile(); } @Override public void onError(SocialIdException e) { // onError code }); }
You can also provide a reset password flow for users with the following code:
import com.coffeebeantech.socialidapi.models.marketing.login.ResetPasswordEmailAttributes; import com.coffeebeantech.socialidapi.util.Language; import com.coffeebeantech.socialidsdk.SocialId; import com.coffeebeantech.socialidsdk.exceptions.SocialIdException; import com.coffeebeantech.socialidsdk.util.SocialIdCallback; ResetPasswordEmailAttributes resetPasswordEmailAttributes = new ResetPasswordEmailAttributes(); resetPasswordEmailAttributes .setEmailAddress(emailAddress) // or setUsername(username) .setRedirectUrl(redirectUrl) // required .setSubject(subject) // optional .setFrom(from) // optional .setDescription(description) // optional .setLogoUrl(logoUrl) // optional .setLanguage(Language.ENGLISH); // optional SocialId.resetUserPassword(resetPasswordEmailAttributes, new SocialIdCallback<Void>() { @Override public void onSuccess(Void v) { // onSuccess code } @Override public void onError(SocialIdException e) { // onError code }); }