Devlog Week 3 – Building a chat app

With my project coming to an end quite soon I’ve been busy with test cases to ensure the application doesn’t suffer any terrible crash and trying to improve reliability. Using Android Studio’s built-in linting feature ( Analyse -> Inspect Code ) can help discover any problems, such as hard-coded strings, unused resources, possible exceptions etc. but memory leaks can be difficult to find.

To take a break from this tedious work I decided to add in a chat feature for my app, where attendees of events can message one another in a channel. I was already using Google Cloud Messaging for push notifications so I tried to devise a way of communication between devices with GCM. Originally I planned to use tags, similar to my notification setup, so that the messages will be received only by those who have the corresponding tag stored in my Azure Notification Hub. I made a Controller in my Web API specifically for this function with some different action methods, but then I thought about how I was going to store the messages on the device, and eventually I thought there must be an easier way (or already done for you way) Enter PubNub.

PubNub is a great service for publish/subscribe messaging which is exactly what I was trying to do myself. It has a free tier which includes a million messages and 100 devices, so it’s perfect for testing the feature. GCM sometimes failed to deliver, or took quite a long period of time for a message to deliver. I have yet to experience this with PubNub, so far it’s been fantastic and is possibly the easiest SDK to set up in an Android project. Simply register (or sign in with Google) on their website and there is more than enough documentation on using it for everyone. So now instead of spending a huge amount of time creating a sub-feature in my project, I have added full chat functionality – which includes a preview of the last message received, the time stamp, and I also send a URL of the profile picture of each person in the object I send via PubNub so my RecyclerView looks like a well-designed Instant-Messaging app. All of this was accomplished within a few hours from start to finish, by following along with the tutorials below.

Android Publish/Subscribe

Android Storage and History