FirePush — A Lightweight Kotlin Library for sending FCM push notifications like a pro.

Karandeep Atwal
3 min readJun 8, 2019

--

Summer’s at its peak and so is the heat, but don’t worry I came up with a coolest kotlin library FirePush.
You may be wandering about what’s the coolest in this ?
Because you can now send push notifications from your android app to any FCM supported platform like a pro.
How like pro ? We will find it later below.
How lightweight?
I have used only Kotlin inbuilt libraries and Coroutines for IO operations.

If you look around googling Send push notification from android device, you will end up with a lot of PHP code on internet and you have to add key pairs in a JSONObject and put them in params and make a http request so and so….

While I was working on a project I have some cases where I have to send push notification from device end e.g like while chatting, event based push and every time I have to hit a http request and wait for the result, Then I thought that there might be a library about the same, I googled , found some java libraries but some were outdated or were not like i was looking for, So I decided to make a library that fits up with my needs. See what I made -

Now, Long story short. For sending push you just need to do the following first.

Step 1. In root build.gradle add -

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

and add below dependency -

dependencies {
implementation 'com.github.karanatwal:FirePush:1.0.0'
}

Step 2. Get Server Key and add below in your Application’s/Activity’s onCreate :

override fun onCreate(savedInstanceState: Bundle?) {  
super.onCreate(savedInstanceState)
Fire.init("YOUR_SERVER_KEY_HERE")
}

You know Server Key? You can found yours on Firebase Console, See -

Step 3 (& Final Step 😎 Isn’t cool ?). Pull your trigger and FIRE..

Fire.create()  
.setTitle("Hello Ladies!!")
.setBody("This is just a body !")
.setBadgeCount(2)
.setClickAction("")
.setAndroidChannelId("")
.setColor("")
.setIcon("")
.setSound("")
.setTag("")
.setPriority(FirePushPriority.HIGH)
.addData("key","value")
.addData(HashMap())
.setCallback { pushCallback, exception ->
//get response here
}
.toTopic("")// or toIds or toCondition
.push()

You can send either to registration tokens or topic or a condition.

On other side you can receive notification in onMessageReceived callback, when your app will be in foreground. And for background System Tray of app will handle it for you. You can get data object in intent of the activity whichever will get opened on click of notification.

Data Payload Only based Push :

Here we can send data only push in which there is no Notification object, only data object with custom key-pairs. Such push notifications directly awakes your android app and onMessageReceived callback is triggered even app is killed.

Note: This happens only in Android. Not for iOS.

For Sending data object only do like below -

Fire.createDataPayloadOnly()  
.setPriority(FirePushPriority.HIGH)
.add("key","value")
.add(HashMap()) //or use this
.setCallback { pushCallback, exception ->
//get response here
}
.toTopic("")// or toIds or toCondition
.push()

You can go through the sample in my Github repo. Thanks, I would appreciate for new Ideas or additions, Pull requests are welcomed.
Karandeep Atwal

--

--