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.
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. 👏