Released an android app template with complete parse.com integration - Yes, it's in public domain
14 July 2015 By Bhavyanshu Parasher
This is a template for android app that uses parse.com as backend.
What to expect in the template?
- Has two types of login (One is for twitter & another is for email + password), register, forgot password, change password, email verification functionality.
- Has AppCompat based custom theme, Support toolbar, English & hindi translations for all string resources.
Of course it is compatible with android studio. The support toolbar is designed according to latest lollipop design specs & is also backward compatible with older OS versions as well. All in all this is a perfect starting point for anyone looking forward to integrating their android app with Parse.
Also the source code is in public domain. You are free to do anything you want with it. More specific terms are in the UNLICENSE document provided with the source code.
How to derive your own app from this template?
Simply import it into your android studio. This post is basically about explaining the components & what each component does.
To begin with, first let us look at the app manifest file.
These permissions are the only permissions that are required for all the authentication work & push notifications.
Then you need to change your package name wherever “me.app.template” is present. There are three instances of it in AndroidManifest.xml
Then we have the application entry point. In the application entry point we basically do all the parse initialization tasks.
Make sure you replace parse_application_ID & parse_client_key in app.xml file. You will get these keys from parse.com when you create a new app.
BaseActivity.java & NoAuthActivity.java are two activities which are extended by other activities. NoAuthAcitivty is to be used by any activity that does not have any authentication data & BaseActivity is to be used for any activity that requires authenticated access. The point of having two is because it makes it easier to display separate menu items for authenticated & non-authenticate users. Both of these extend the AppCompatActivity. So you only need to extend any of these from some other activity & it will inherit everything.
Like in LoginActivity.java
& when logged in, it will start MainActivity.java which uses BaseActivity.java
Tables modified in the database
The columns mentioned below are the ones I have added and will not exist in default app setup on parse.com so make sure you add these.
Table name : _User -> screenName (String)
Table name : tw -> keyid (String) , secretid (String)
Twitter login & signup
The normal login is easy. That should work as it is. Just make sure you have added above mentioned columns in the _User table. The real deal is twitter. I store keys where I can easily & quickly modify them in case I revoke them. So I use parse table for that purpose.
If you followed database schema provided in previous step, you should have a table “tw” & string type columns “keyid” & “secretid”. As you can see in LoginActivity.java I have written a parse query to get these values
The benefit of this method is that if you need to revoke twitter keys, you just need to quickly modify them in the table only. The app will query this data. Moreover, you should avoid adding important keys directly as strings in android app. Apps can be reverse engineered & the keys can be misused. In above steps, I have shown parse keys being added as strings. It’s wrong as well. What you should do is, if you own a server, then send a request to your own server & get these keys first. Then use them. It’s just one added layer of obfuscation. Doesn’t mean it is a completely secure method. Rather it just gets time consuming for the hacker to get to the keys. If you want to avoid doing it this way, then simply remove whole of the above code with just this one line (shown below) & pass your keys as parameters but with this you will have to push an app update in case you revoke your twitter keys.
The SignIn & SignUp are both handled by signInTwitter function called when Log In with Twitter button is pressed.
As you can see I have used additional column in _User table. That is called screenName. Had to use this because when parse pulls the data from twitter, the data added to username column in _User table is hashed but ParseTwitterUtils provides a way to get the real username & it is via .getScreenName(); function as shown above.
One more thing. In case you need to check whether the user has verified email or not, just use this code in your respective activity’s onCreate
Twitter does not allow extracting email addresses. Hence, the values of column “email” are only set for people who register using email and not for those who register via twitter. Therefore checking containsKey(“email”) is very important otherwise it will return undefined value for twitter users & they will be sent back to Login Acitivty. Also make sure you turn on email verification in your parse settings.
AppCompat & Toolbar support
To get the toolbar functionality, you need to have toolbar in the layout file. Like
This is backward compatible with android versions prior to lollipop. So you don’t have to worry about compatibility issues. This method is used to custom design actionbars for Lollipop. Look at toolbar.xml & styles.xml for more details. Now in YourActivity.java just add the below code to use toolbar wherever you want.
That’s all there is. I hope you are able to use this easily with the above mentioned customizations. In case you encounter a problem, leave a comment below.
Refer Table of contents for all android tutorials.
blog comments powered by Disqus