Writing mobile software is one of the hottest skills that can fetch you a job. With the entire ecosystem being driven by two large platforms namely iOS and Android, it’s indeed the best time to be a developer.
Assuming that you have some amount of programming experience already under your belt, app development won’t be overwhelming for you. And it will indeed be a learning experience, I tell this from personal experience, learning to build on top of a platform opens your mind up to a whole new way of thinking. Because now you’re not only managing what’s happening in the foreground and background of your app, you are also managing its different invisible states and making sure it’s being a “good citizen” of the ecosystem that’s is your user’s device.
This article is a part of an ongoing series here at TechLila where we’ll focus on how you can be a better developer by spending time on building experiences instead of cracking your head thinking about problems that have already been solved elegantly (I’ll come to that in just a bit). Do note however that this series is not for beginners, we’ll have something in store for you soon enough. This series focuses on helping out developers who already have a fair share of expertise in Android.
This article is the first in a series of articles that we’ll be publishing to help Android developers add key “engaging” features to their apps by leveraging existing infrastructure instead of worrying about building it from scratch.
Push Notifications on Android
In this first one, we’ll focus on Push Notifications. Push Notifications fall into the engagement bracket of software integration.
Alright so assume you just completed building the next epic app which is totally killer and you are extremely optimistic about its future. In the first few days, the app is doing really well. The stability is top notch and the downloads just keep increasing and you can’t keep your eyes on the metrics. But soon enough you start noticing a very strange pattern in your app’s usage. While it’s managing to attract new users to its self but it’s losing the existing users. Retention is just dropping every day. You get all anxious worrying about what to do now, you thought people would really like it. But it’s just nothing working out.
If you’re in such a situation then this post might justifications help you out a bunch. The problem isn’t your app. It’s the user’s phone. Let me explain. Your idea is a killer and if you’re confident about its viability then all you should care about is fixing the retention problem. So yeah, I’m your user and I have a typical Android phone. With a decent 32 GB storage and thanks to Reliance Jio, a pretty stable internet connection. What do I do? I go and explore what’s out there for me to consume. In this process, I just choke up my device with 100s of apps and while it’s true that I really liked your app and found it to be interesting, honestly at this point I’ve forgotten about it. It still exists on my phone but I just don’t notice it that well among all the other apps that are there.
The problem here is your app isn’t engaging enough. It just reminds users that it’s there sitting on their device waiting for them to use it and among all the attention freak apps like Facebook and WhatsApp constantly bombarding your users with notifications it’s very likely that your app will get lost in the crowd unless it fixes the engagement problem.
How to Add Push Notifications to Your Android Project
Push Notification is a great lightweight mechanism to fix the problem and this article will show you just how to implement it in your own projects.
We are going to use IBM’s Bluemix for integrating the Push Notifications into our apps. There are a ton of other providers which will give you an SDK to integrate into the project and go about developing it. But there are a couple of key reasons why I choose Bluemix over others.
- First Bluemix offers a ton of more features that I can add to an existing project with zero hassled (push notifications happen to be just one of those offerings). Their console makes it easy to manage all their integrations.
- Second, if you are a beginner and try out any other SDK you most likely will get stuck somewhere (I did). With Bluemix that’s hardly the case since theory documentation and guides are so great.
- Third and one of the most important reasons to choose Bluemix is that even though it is a paid service, it comes with a 30 days trial so you can play with all their offerings before deciding to commit to their platform and invest in it. It’s always a good practice to test some things out before investing in it.
- Fourth and this is more like brownie points to IBM, there’s a sense of familiarity and trust developers have associated with IBM as a company. They’ve been doing some amazing work in the technology area for quite some time now and they know their stuff well, so you can jump ship without any worries
To proceed any further with this lesson any further we first need to make sure that we all are on the same page so before diving any further into this post make sure you have signed up for Bluemix (if you haven’t already) and logged in to the console.
This article is focused on Android, so I would assume some familiarity with writing apps for Android on your part. Alright, let’s get started.
Step 1: The Initial Setup
Open an existing project in Android Studio in which you want to integrate Push Notifications. You can also go ahead and create a fresh project just to test things out. If you are opening an existing project, make sure you switch to a new branch before making any additions to your project. Once you are done with this step, build the project and deploy it on your phone/emulator. This will just prove that everything is working as expected and we are good to go.
Step 2: Retrieve your Firebase Cloud Messaging API Key
Bluemix leverages Google Cloud Messaging to send downstream push notifications to your app. By taking over your Firebase Cloud Messaging (GCM) Project it just takes a huge hassle off your hands of setting and configuring things for you. Head over to console.firebase.google.com, and Login using a Google Account then follow the steps below
- Create a new Project.
- Give it a name.
- Once it is setup go to Project Settings.
- Click on Cloud Messaging.
- Copy the Server API Key and the Sender ID as shown in the screenshot below.
- After the above steps are completed, proceed further and add Firebase to your Android Project. This step involves entering your project’s package name and other details and then downloading the “google-services.json“ file.
NOTE: It’s important that you complete this step correctly to have the upcoming steps working properly. Make sure your app is configured and the “google-services.json” file is placed in your project.
Step 1: Register App
Step 2: Download Config File
Step 3: Add Firebase SDK
Make sure you run your project at this stage just to see if everything is working properly. If everything is good to proceed to the next step.
Step 3: Configure a Bluemix Service
- Head over to your Bluemix Console.
- Open your Bluemix catalog and then click the IBM Push Notifications for Bluemix service instance you have created.
- Click on Manage and then go to Configure.
- Select Mobile.
- Now enter the credentials you just copied in the previous step over here. Update the GCM/FCM Push Credentials tab with the Sender ID/Project number and API Key.
- Hit Save.
Step 4: Sending a Push!
- Alright, we’re almost done. Deploy your app on your phone/emulator and now head over to your console.
- Select Send Notification.
- Configure your message on this section.
- Once everything is done, hit Send and head over to your device.
Bluemix has some additional options for Android as per their documentation. You can extensively configure your notification. Here are the options (directly from their docs):
- Collapse Key: Collapse keys are attached to notifications. If multiple notifications arrive sequentially with the same collapse key when the device is offline, they are collapsed. When a device comes online, it receives notifications from the FCM/GCM server, and displays only the latest notification bearing the same collapse key. If the collapse key is not set, both the new and old messages are stored for the future delivery.
- Sound: Indicates a sound clip to be played on the receipt of a notification. Supports default or the name of a sound resource that is bundled in the app.
- Icon: Specify the name of the icon to display for the notification. Ensure that you have packaged the icon in the
res/drawablefolder, with the client application.
- Priority: Specifies the options for assigning delivery priority to messages.
- A priority
maxwill result in heads-up notification.
- A priority
defaultwill not open network connections on a sleeping device.
- A priority
minwill be a silent notification.
- A priority
- Visibility: You can choose to set the notification visibility option to either
privateoption restricts public viewing and you can choose to enable it if your device is secure with a pin or pattern, and the notification setting is set to Hide sensitive notification content. When the visibility is set as
redactfield must be mentioned. Only the content specified in the
redactfield will show up on a securely locked screen on the device.
publicoption would render the notifications to be freely read.
- Time to live: This value is set in seconds. If this parameter is not specified, the FCM/GCM server stores the message for four weeks and will try to deliver. The validity expires after four weeks. The possible value range is from 0 to 2,419,200 seconds.
- Delay when idle: You can set this to either of the following values:
Trueinstructs the FCM/GCM server not to deliver the notification if the device is idle.
Falseensures notification delivery even if the device is idle.
- Sync: By setting this option to
true, notifications across all your registered devices are in sync. If the user with a username has multiple devices with the same application installed, reading the notification on one device ensures deletion of notifications in the other devices. You need to ensure that you are registered with Push Notifications service with userId for this option to work.
- Additional payload: Specifies the custom payload values for your notifications.
- Expandable notification: This provides customers an option to expand a notification with more information, while a basic notification would be visible with the notification collapsed. The following options are supported:
- Big Picture Notifications: You can choose to include a picture when the notification is expanded. Ensure that you provide a Title text and URL for the picture.
- Big Text Notifications: You can choose to include additional text with a title. Ensure that the Big Text message and Title text information is furnished.
- Inbox Style Notifications: You can send the notification styled as an inbox notification. Provide a Title text and furnish the message in Lines.
That’s it! just like that you set up push notifications in your app in four easy steps without going through the hassle of procuring a server and bothering about unnecessary complexities. This is how easy developing with IBM Bluemix becomes. Push notification is just one of their offerings. They offer databases and Watson APIs among the many. And since the Bluemix console ties everything together you don’t need to keep shuffling between services. It’s all under one roof. I hope that this article has helped you to learn something new and if you are interested in learning more things like these, I would highly encourage you to try Bluemix.