0% found this document useful (0 votes)
32 views

Listening for UI Notification

The document explains how to create and manage notifications in Android applications, detailing various types of notifications such as status bar and heads-up notifications. It provides a step-by-step guide on using NotificationListenerService and BroadcastReceiver to listen for UI notifications, along with example implementations in Java. Additionally, it outlines potential use cases for listening to notifications, including tracking app notifications and automating tasks.

Uploaded by

laibaahsan924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Listening for UI Notification

The document explains how to create and manage notifications in Android applications, detailing various types of notifications such as status bar and heads-up notifications. It provides a step-by-step guide on using NotificationListenerService and BroadcastReceiver to listen for UI notifications, along with example implementations in Java. Additionally, it outlines potential use cases for listening to notifications, including tracking app notifications and automating tasks.

Uploaded by

laibaahsan924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Notifications in Android with Example

Notification is a kind of message, alert, or status of an application (probably

running in the background) that is visible or available in the Android’s UI

elements. This application could be running in the background but not in use

by the user. The purpose of a notification is to notify the user about a process

that was initiated in the application either by the user or the system. This

article could help someone who’s trying hard to create a notification for

developmental purposes.

Notifications could be of various formats and designs depending upon the


developer. In General, one must have witnessed these four types of
notifications:

1. Status Bar Notification (appears in the same layout as the current


time, and battery percentage)
2. Notification drawer Notification (appears in the drop-down menu)
3. Heads-Up Notification (appears on the overlay screen, ex: WhatsApp
notification, OTP messages)
4. Lock-Screen Notification (I guess you know it)

In Android mobile development, listening for UI notifications generally


refers to detecting and responding to system or app-generated
notifications. This can be done using NotificationListenerService or
event-driven mechanisms such as BroadcastReceivers.

1. Using NotificationListenerService

Android provides the NotificationListenerService class, which


allows your app to listen for all notifications that appear in the status bar.
Steps to Listen for UI Notifications:

1. Create a Service that Extends NotificationListenerService


○ This service will listen for all system notifications.
2. Register the Service in AndroidManifest.xml
○ The service must be declared in the manifest with the proper
permissions.
3. Handle Notifications in the Service
○ Override onNotificationPosted() and
onNotificationRemoved() to respond to notifications.

Example Implementation
1. Create a Notification Listener Service
java
CopyEdit
import
android.service.notification.NotificationListenerServic
e;
import
android.service.notification.StatusBarNotification;
import android.util.Log;

public class MyNotificationListenerService extends


NotificationListenerService {
private static final String TAG =
"NotificationListener";

@Override
public void
onNotificationPosted(StatusBarNotification sbn) {
Log.d(TAG, "Notification received: " +
sbn.getPackageName());
}

@Override
public void
onNotificationRemoved(StatusBarNotification sbn) {
Log.d(TAG, "Notification removed: " +
sbn.getPackageName());
}
}

2. Declare the Service in AndroidManifest.xml


xml
CopyEdit
<service
android:name=".MyNotificationListenerService"

android:permission="android.permission.BIND_NOTIFICATIO
N_LISTENER_SERVICE">
<intent-filter>
<action
android:name="android.service.notification.Notification
ListenerService" />
</intent-filter>
</service>

3. Grant Notification Access

● Go to Settings → Apps & notifications → Special app access →


Notification access, then enable it for your app.

2. Using a BroadcastReceiver for Specific UI Events

Instead of listening to all notifications, you can listen to specific system UI


events using BroadcastReceiver.

Example: Listening for SMS Notifications

java
CopyEdit
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent
intent) {
if
(intent.getAction().equals("android.provider.Telephony.
SMS_RECEIVED")) {
Log.d("SmsReceiver", "SMS received!");
}
}
}

Declare in AndroidManifest.xml:

xml
CopyEdit
<receiver android:name=".SmsReceiver">
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED"
/>
</intent-filter>
</receiver>

Use Cases of Listening for UI Notifications

● Tracking App Notifications: Listen to incoming WhatsApp, Gmail,


or other app notifications.
● Automating Tasks: Trigger actions when a notification appears
(e.g., dismissing low-battery notifications).
● Accessibility Services: Assisting visually impaired users by reading
notifications aloud.
● Logging Notifications: Capturing and storing notifications for
analytics or debugging.

You might also like