Back to search

Build a cryptocurrency alert app using Kotlin and Go: Part 1 - The frontend

  • Neo Ighodaro
July 21st, 2018
You will need Android Studio 3+ and Go 1.10.2+ installed on your machine. You should have some familiarity with Android development and the Kotlin language.

Cryptocurrency is one of the hot topics today and as a result of this, many people have purchased many cryptocurrencies. However, the market is unpredictable and changes very often, so people tend to keep an eye on the changes in price of their asset.

In this post, we will create an app that watches for changes in the value of cryptocurrencies in realtime and notifies the user when the changes occur. We will focus on two very popular cryptocurrencies - Bitcoin and Ethereum. When we are done, your phone will receive a push notification when the value of Bitcoin and Ethereum either exceeds or goes below a value you specify in the settings of the app.

Here is a screen recording of what we will build:



To follow along, you need the following installed:

Building our Android app

Creating our Android application

First, launch Android Studio and create a new application. Enter the name of your application, for example, CryptoAlert and then enter the package name. Make sure the Enable Kotlin Support checkbox is selected. Choose the minimum SDK, click Next, choose an Empty Activity template, stick with the MainActivity naming scheme and then click Finish.


Creating Pusher Beams application

Getting your FCM key and Google services file

Since Pusher Beams relies on Firebase, we need an FCM key and a google-services.json file. Go to your Firebase console and click the Add project card to initialize the app creation wizard.

Add the name of the project, for example, crypto-`alert`, read and accept the terms of conditions. After this, you will be directed to the project overview screen. Choose the Add Firebase to your Android app option.

The next screen will require the package name of your app. You can find your app’s package name in your app-module build.gradle file. Look out for the applicationId value. Enter the package name and click Next. You will be prompted to download a google-services.json file. Download the file and skip the rest of the process. Add the downloaded file to the app folder of your project - name-of-project/app.

To get the FCM key, go to your project settings on Firebase, under the Cloud messaging tab you should see the server key.


Next, login to the new Pusher dashboard. You should sign up if you don’t have an account yet.

Create a new Pusher Beams instance

Open your Pusher Beams dashboard and create a new Pusher Beams application.


After creating your instance, you will be presented with a quick-start guide. Select the Android quick-start. After you add the FCM key, you can exit the quick-start guide.

Adding functionalities to our app

For our app to work, we need to pull in a couple of dependencies. To do this, add the following to the project build-gradle file:

    // File: ./build.gradle
    buildscript {
        // [...]

        dependencies {
            // [...]

            classpath ''

    // [...]

Next, add the following to the app module build.gradle file:

    // File: ./app/build.gradle
    dependencies {
        implementation 'com.squareup.retrofit2:retrofit:2.4.0'
        implementation 'com.squareup.retrofit2:converter-scalars:2.4.0'
        implementation ''
        implementation 'com.pusher:push-notifications-android:0.10.0'

    // Add this line to the end of the file
    apply plugin: ''

Above we included Retrofit - a package for making network calls, and then the Pusher Beams package for sending push notifications. The additional Google services are dependencies for the Pusher Beams package. Sync your gradle files to make the libraries available for use.

Next, create a new interface named ApiService and paste the code below:

    // File: ./app/src/main/java/{package-name}/ApiService.kt

    import okhttp3.RequestBody
    import retrofit2.Call
    import retrofit2.http.Body
    import retrofit2.http.GET
    import retrofit2.http.POST

    interface ApiService {

      fun saveBTCLimit(@Body body: RequestBody): Call<String>

      fun saveETHLimit(@Body body: RequestBody): Call<String>

      fun getValues():Call<String>


This file is used to by Retrofit to know the endpoints to be accessed. The first endpoint /btc-pref is used to set the Bitcoin limits. The next endpoint /eth-pref is used to save the Ethereum limits. The last endpoint /fetch-values is used to get the current values of the cryptocurrencies.

To make use of network services in your application, add the internet permission in your AndroidManifest.xml file like so:

    <manifest xmlns:android=""

      <uses-permission android:name="android.permission.INTERNET"/>


Next, we will manage notifications in our app. Create a new service named NotificationsMessagingService and paste this:

    // File: ./app/src/main/java/{package-name}/NotificationsMessagingService.kt
    import android.content.Intent
    import android.os.Build
    import com.pusher.pushnotifications.fcm.MessagingService

    class NotificationsMessagingService : MessagingService() {

        override fun onMessageReceived(remoteMessage: RemoteMessage) {
            val notificationId = 10
            val channelId  = "crypto_channel"
            lateinit var channel: NotificationChannel
            val intent = Intent(this,
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

            val mBuilder = NotificationCompat.Builder(this, channelId)

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val notificationManager = applicationContext.getSystemService(
                val name = getString(R.string.channel_name)
                val description = getString(R.string.channel_description)
                val importance = NotificationManager.IMPORTANCE_DEFAULT
                channel = NotificationChannel("crypto_channel", name, importance)
                channel.description = description
            } else {
                val notificationManager =  NotificationManagerCompat.from(this)

Because there are major changes to push notifications in Android O, we checked for the Android version before handling the notification. If we are using Android O or newer, we have to create a notification channel that will be used to categorize the type of notification we are sending. This is particularly useful for apps that send different types of notifications.

We also made use of some files stored in the strings.xml file to describe the notifications channel description and channel name. Add these to the strings.xml file:

    <!-- File: /app/src/main/res/values/strings.xml -->
    <string name="channel_name">Crypto</string>
    <string name="channel_description">To receive updates about changes in cryptocurrency value</string>

Register the service in the AndroidManifest.xml file:

      <service android:name=".NotificationsMessagingService">
          <intent-filter android:priority="1">
              <action android:name="" />

Now, let’s prepare our layouts. First, we will design the activity’s layout. When creating your app, the activity_main.xml file should already be present in the layout folder. Open it and replace with this:

    <!-- File: ./app/src/main/res/layout/activity_main.xml -->
    <?xml version="1.0" encoding="utf-8"?>
    < xmlns:android=""

        android:text="1 BTC"
        app:layout_constraintTop_toTopOf="parent" />

        android:text="1 ETH"


The layout contains two TextViews to show prices for Bitcoin and Ethereum. We also made these TextViews clickable so we can set limits to get notifications when the limits are surpassed.

Next, we will design the layout of our alert dialog. Create a new layout file named alert_layout and paste this:

    <!-- File: ./app/src/main/res/layout/alert_layout.xml -->
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
      android:orientation="vertical" android:layout_width="match_parent"

        android:hint="Minimum value"
        android:layout_height="60dp" />

        android:hint="Maximum value"
        android:layout_height="60dp" />

        android:layout_height="wrap_content" />


This will be the layout showed by the dialog. It contains two text fields and a save button and we used a custom designed background for the TextViews. Create a new drawable file named text_background and paste this:

    <!-- File: /app/src/main/res/drawable/text_background.xml -->
    <shape xmlns:android="" android:shape="rectangle" >
      <solid android:color="@android:color/white" />
      <stroke android:width="1dip" android:color="@android:color/darker_gray"/>

We will move to the MainActivity to finish up our app’s functionalities. Open your MainActivity and replace the contents with the following:

    // File: ./app/src/main/java/{package-name}/MainActivity.Kt
    import android.os.Bundle
    import okhttp3.MediaType
    import okhttp3.RequestBody
    import org.json.JSONObject
    import retrofit2.Call
    import retrofit2.Callback
    import retrofit2.Response
    import android.util.Log
    import android.view.LayoutInflater
    import android.widget.Button
    import android.widget.EditText
    import com.pusher.pushnotifications.PushNotifications
    import okhttp3.OkHttpClient
    import retrofit2.Retrofit
    import retrofit2.converter.scalars.ScalarsConverterFactory

    class MainActivity : AppCompatActivity() {

        private var prefs: Prefs? = null

        private val retrofit: ApiService by lazy {
            val httpClient = OkHttpClient.Builder()
            val builder = Retrofit.Builder()

            val retrofit = builder

        override fun onCreate(savedInstanceState: Bundle?) {

The URL used above,, is the URL the Android emulator recognizes as localhost.

Above, we created a retrofit object to be used for network calls. After setting up the retrofit object, we add the layout in the onCreate method and call three other functions:

  1. fetchCurrentPrice - This function will get the current price of Bitcoin and Ethereum from our server. Create a new function within the class and set it up like so:
    // File: /app/src/main/java/{package-name}/MainActivity.kt
    private fun fetchCurrentPrice() {
        retrofit.getValues().enqueue(object: Callback<String> {
            override fun onResponse(call: Call<String>?, response: Response<String>?) {
                val jsonObject = JSONObject(response!!.body())
                bitcoinValue.text = "1 BTC = $"+ jsonObject.getJSONObject("BTC").getString("USD")
                etherumValue.text = "1 ETH = $"+ jsonObject.getJSONObject("ETH").getString("USD")

            override fun onFailure(call: Call<String>?, t: Throwable?) {

Above, a network call is made to get the current Bitcoin and Ethereum prices in USD. When the response is received, we parse the JSON data and display it on the screen by setting the texts of the text views in the layout.

  1. setupPushNotifications - This function is where we start listening to the interest of our choice to receive notifications. The interest name is in this format {device_uuid}_{currency}_changed. **We register two interests, one for each currency. Open the MainActivity class and add the following method:
    // File: /app/src/main/java/{package-name}/MainActivity.Kt
    private fun setupPushNotifications() {
        PushNotifications.start(applicationContext, "PUSHER_BEAMS_INSTANCE_ID")
        val fmt = "%s_%s_changed"
        PushNotifications.subscribe(java.lang.String.format(fmt, deviceUuid(), "BTC"))
        PushNotifications.subscribe(java.lang.String.format(fmt, deviceUuid(), "ETH"))

Replace PUSHER_BEAMS_INSTANCE_ID with the instance ID found on your Pusher Beams dashboard.

  1. setupClickListeners - In this function, we will set up click listeners to the text views in our layout. In the same MainActivity class, add the following method:
    // File: /app/src/main/java/{package-name}/MainActivity.Kt
    private fun setupClickListeners() {
        bitcoinValue.setOnClickListener {

        etherumValue.setOnClickListener {

When any of the text views is clicked, we call the createDialog method which then opens up a layout for the user to input the limit.

In the MainActivity class, add the method createDialog and as seen below:

    // File: /app/src/main/java/{package-name}/MainActivity.Kt
    private fun createDialog(source:String){
      val builder: AlertDialog.Builder = AlertDialog.Builder(this)
      val view = LayoutInflater.from(this).inflate(R.layout.alert_layout,null)

      builder.setTitle("Set limits")

      val dialog = builder.create()
      val minEditText: EditText = view.findViewById(
      val maxEditText: EditText = view.findViewById(

      view.findViewById<Button>( {
        if (source == "BTC"){
          saveBTCPref(minEditText.text.toString(), maxEditText.text.toString())
        } else {
          saveETHPref(minEditText.text.toString(), maxEditText.text.toString())

This dialog gets the minimum and maximum values and sends it to the backend server. This is done so that when the cryptocurrency’s price changes, we’ll get a push notification if it is within the limits set.

In the function above, we called two new methods. Add the two methods in the MainActivity class as seen below:

    // File: /app/src/main/java/{package-name}/MainActivity.Kt
    private fun saveBTCPref(min:String, max:String){
        val jsonObject = JSONObject()
        jsonObject.put("minBTC", min)
        jsonObject.put("maxBTC", max)
        jsonObject.put("uuid", deviceUuid())

        val body = RequestBody.create(

        retrofit.saveBTCLimit(body).enqueue(object: Callback<String> {
            override fun onResponse(call: Call<String>?, response: Response<String>?) {}
            override fun onFailure(call: Call<String>?, t: Throwable?) {}

    private fun saveETHPref(min:String, max:String){
        val jsonObject = JSONObject()
        jsonObject.put("uuid", deviceUuid())

        val body = RequestBody.create(

        retrofit.saveETHLimit(body).enqueue(object: Callback<String> {
            override fun onResponse(call: Call<String>?, response: Response<String>?) {}
            override fun onFailure(call: Call<String>?, t: Throwable?) {}

In the saveBTCPref and saveETHPref we attempt to send the limits set by the user to the API so it can be saved for that user.

While sending, we also send the uuid which is the devices’ unique identifier. Let’s create the deviceUuid() method that will generate and save this UUID per device. In the MainActivity class, add the following code:

    // File: /app/src/main/java/{package-name}/MainActivity.Kt
    private fun deviceUuid() : String {
        prefs = Prefs(this)
        var uuid: String = prefs!!.deviceUuid

        if (uuid == "") {
            uuid = java.util.UUID.randomUUID().toString().replace("-", "_")
            prefs!!.deviceUuid = uuid

        return uuid

Now in this function, we reference a Prefs class. Create a new Prefs class and paste the following code into it:

    // File: /app/src/main/java/{package-name}/Prefs.Kt
    import android.content.Context
    import android.content.SharedPreferences

    class Prefs (context: Context) {
        val PREFS_FILENAME = "com.example.coinalert.prefs"
        val DEVICE_UUID = "device_uuid"
        val prefs: SharedPreferences = context.getSharedPreferences(PREFS_FILENAME, 0);

        var deviceUuid: String
            get() = prefs.getString(DEVICE_UUID, "")
            set(value) = prefs.edit().putString(DEVICE_UUID, value).apply()


That’s all for the application. At this point, the application should build successfully, but, not function as intended. In the next part, we will build the backend of the application so it can work as intended.


In this article, we have learned how to use Pusher Beams to notify users of changes to a cryptocurrency. You can find the repository for the application built in this article here.

  • Beams

© 2018 Pusher Ltd. All rights reserved.

Pusher Limited is a company registered in England and Wales (No. 07489873) whose registered office is at 28 Scrutton Street, London EC2A 4RP.