AndroidPub
Published in

AndroidPub

How to Change App Status with a Notification (Android)

If you want your users to be able to interact with your android app even if it is not on the foreground, the notification is at your service. Here comes the step by step guideline.

Step 1 — Create a Notification Channel

Android requires you to register all of your notifications to a notification channel for Android 8.0 (API level 26) and later versions. No channel, no notification… Let’s create a channel.

It is safe to call this method on your “onCreate” method if you know that you are going to create the channel anyway. Calling ‘createNotificationChannel’ method multiple times is also safe. If there is a channel with the same id it is not going to be created again. (name and priority of a channel can be modified but it is another topic)

Step 2 — Create a simple notification

Use NotificationCompat class to not care about the android version of the device. Here comes the sample;

We have a notification now, but it only displays its “small icon”, “title”, and “text” (description). No interaction yet.

Step 3 — Add pending intent

You can get events from notifications by using ‘Intent’s inside of ‘PendingIntent’s since they are waiting to be triggered. In our scenario, we are going to use an Intent object that has a BroadcastReceiver class in it because we want it to be executed even if the app is not on the foreground. I’ll go one step further and create my BroadcastReceiver class and put a static factory method for its PendingIntent creation.

We know how to get our PendingIntent now. Let’s add it to our notification.

Step 4— Add pending intent to notification builder

Step 5— Add the BroadcastReceiver class to AndroidManifest

This one is so short but crucial. Without adding your broadcast receiver to the AndroidManifest you won’t be able to receive click events.

<receiver android:name=".InteractiveNotificationBroadcastReceiver" />

Put this line into your <application></application> tags.

If everything goes well you should be able to see “D/InteractiveNotificationBroadcastReceiver: onReceive” message in your Logcat.

Step 6— Update the notification

Now all you have to do is creating a new notification with the same channel id and notification id with the new content. Here, the following is how your onReceive method should look like.

Clicking on a notification triggers the intent that we set as the content. It also makes the notification go away from the screen, which is kind of annoying if you want to keep interacting with it. Android has a solution for it.

Adding action to your notification

Adding action to a notification is almost same as setting the content intent of the notification.

Requirements to add an action;

  • An icon resource id
  • Action title (Action button text)
  • Pending intent (that has the intent that will be triggered when the action happened.

Here is the method that will be added to your notification builder chain.

.addAction(
R.drawable.ic_launcher_foreground,
"Increment",
InteractiveNotificationBroadcastReceiver.newPendingIntent(context)
)

I hope it was a straight forward guide. If you want to see the whole code or change/add some more code to this sample, feel free to go ahead and create a pr for it at here;

Any comment, claps, stars, and suggestions are welcome. 👏

--

--

--

The (retired) Pub(lication) for Android & Tech, focused on Development

Recommended from Medium

How I integrated Native Ads with PagedListAdapter in 7 steps

Material Motion with MDC

#4 CircularProgressIndicator and LinearProgressIndicator(Flutter Famous Widgets’ Guide)

Pure and View Functions in Solidity

Android Save and Restore Instance State: A Writeup about My Journey into the Unknown.

Improve the UI Performance of Your Android App

How to Fake Your Location Programmatically and Avoid Being Tracked by Companies

Using the Navigation Component in a Modular World

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
efe budak

efe budak

Android Developer @Teckro.com

More from Medium

How does Cloud Firestore work?

How to pass params on App Switch from Cordova to KOTLIN App

Language Change Programmatically in Kotlin, Grow Your Apps Audience Using Localization…

Say Hello 👋 to Jetpack Compose and Compare with XML