I'm working on a notification based app, for which I need to listen to incoming notifications. I've been able to listen to incoming calls, SMS, mail etc. I have no clue how to listen for pings or messages from friends on Whatsapp via code. Can this actually be done? If so, how? Can Accessibility Service be used for this, using Package Name as "com.whatsapp"?

- 20,585
- 22
- 95
- 108

- 4,276
- 6
- 44
- 69
-
whatsapp is your on app or third party ? – ρяσѕρєя K Jan 26 '13 at 18:57
-
1whatsapp is a third party messaging/chat app. I don't think it can be done, do you? ^^^ – Jade Byfield Jan 26 '13 at 18:59
-
Using NotificationListenerService we can easily do this. check complete demo code http://www.androiddevelopersolutions.com/2015/05/android-read-status-bar-notification.html – Mukesh Y May 31 '15 at 18:57
-
@ Mukesh Y this only work on API18 and above. – yshahak Sep 20 '15 at 05:29
5 Answers
I was able to do this using Accessibility Service . Using this, you can listen to all notification on the notification bar. I listened to application-specification by adding the package name to the Accessibility Service service info
, which in this case was com.whatsapp
. I couldn't read the messages, but I get notified whenever a message arrives.

- 4,083
- 1
- 27
- 37

- 4,276
- 6
- 44
- 69
-
Can you mute whatsapp notification sound and set a custom notification according to the sender of the message? – frankish Aug 17 '13 at 17:30
-
2@frankish : Absolutely.......NO you can not show custom notification for different users, through your app, but to mute Whatsapp is impossible unless you have made whatsapp – Bhavana Vadodariya Sep 18 '13 at 10:21
-
1@sanjeev mk using accesbility code can i find either user has recieved messages or recieved images /audio files – Erum Dec 27 '14 at 07:36
-
-
@sanjeev can you please share your code how you have implemented this? – Aman Jain Sep 14 '17 at 17:02
-
2
-
here is the correct link : https://developer.android.com/reference/android/accessibilityservice/AccessibilityService , suggestion edit queue is full – Maifee Ul Asad Mar 22 '21 at 14:23
Aug 11, 2020 Update
You can use NotificationListenerService
to listen for notifications and get contents.
Here's example implementation in Java:
public class NotificationListener extends NotificationListenerService {
private static final String TAG = "NotificationListener";
private static final String WA_PACKAGE = "com.whatsapp";
@Override
public void onListenerConnected() {
Log.i(TAG, "Notification Listener connected");
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
if (!sbn.getPackageName().equals(WA_PACKAGE)) return;
Notification notification = sbn.getNotification();
Bundle bundle = notification.extras;
String from = bundle.getString(NotificationCompat.EXTRA_TITLE);
String message = bundle.getString(NotificationCompat.EXTRA_TEXT);
Log.i(TAG, "From: " + from);
Log.i(TAG, "Message: " + message);
}
}
To enable the service insert the following code snippet into <application>
tag on AndroidManifest.xml:
<service
android:name=".NotificationListener"
android:label="@string/notification_listener_service"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
To intercept notifications, you need to request a special permission. Here's the code which you can use to open settings page where user have to allow the app to listen to incoming notifications. On my project I placed it in MainActivity.onCreate
method.
startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS))
If you need to check whether or not the notification listener permission is granted using the following code snippet:
private boolean isNotificationServiceEnabled(){
String pkgName = getPackageName();
final String flat = Settings.Secure.getString(getContentResolver(),
ENABLED_NOTIFICATION_LISTENERS);
if (!TextUtils.isEmpty(flat)) {
final String[] names = flat.split(":");
for (String name: names) {
final ComponentName cn = ComponentName.unflattenFromString(name);
if (cn != null) {
if (TextUtils.equals(pkgName, cn.getPackageName())) {
return true;
}
}
}
}
return false;
}
Old answer
I listened to incoming WhatsApp message notifications with the help of this 2 parted article which you can read it from here.
- Configure AndroidManifest.xml
<!-- AndroidManifest.xml -->
<service
android:name=".MyAccessibilityService"
android:enabled="true"
android:exported="true"
android:label="My application"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/serviceconfig" />
</service>
- Create a new file called
serviceconfig.xml
in /xml/ directory.
<!-- serviceconfig.xml -->
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagDefault"
android:canRequestEnhancedWebAccessibility="true"
android:notificationTimeout="100"
android:packageNames="@null"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true" />
- Create a new
MyAccessibilityService
class which extendsAccessibilityService
.
@Override
protected void onServiceConnected() {
System.out.println("onServiceConnected");
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
info.notificationTimeout = 100;
info.packageNames = null;
setServiceInfo(info);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
if (event.getPackageName().toString().equals("com.whatsapp")){
StringBuilder message = new StringBuilder();
if (!event.getText().isEmpty()) {
for (CharSequence subText : event.getText()) {
message.append(subText);
}
// Message from +12345678
}
}
}
}
- It's ready. Now you can customize the service for your needs.
Note: Because accessibility services are able to explore and interact with on-screen content, a user has to explicitly enable services in Settings > Accessibility. More details
Note
To send replies to received notifications check out this answer.

- 4,083
- 1
- 27
- 37
-
ok so i have to get the deleted msg only, how to do that in your case it will fetch all incoming whatsapp msg – Sunil Chaudhary Mar 17 '20 at 07:04
-
@SunilChaudhary whatsapp hides notification if message was deleted so you can listen for notification state changes and detect if it dismissed by whatsapp or user. I'm not sure about that. – TheMisir Mar 17 '20 at 22:44
-
-
1@newbieee I updated the answer with additional details, please check it – TheMisir Aug 11 '20 at 15:47
-
I'm using the notification listener service but I couldn't able to identify if the notification is from a group conversation or not in android 6 [API 23]. can you help me with this? Here is the detailed Question. https://stackoverflow.com/q/63913290/9944838 – Vijay Sep 16 '20 at 06:02
-
-
I am using notification service but problem is that after rebooting the device it does not start what i have to do please help me [here is my code](https://stackoverflow.com/questions/64559125/notificationlistenerservice-not-working-after-device-boot-complete) – Prototype Oct 28 '20 at 07:55
-
@prototype86 check out this https://stackoverflow.com/questions/58362719/how-can-i-start-android-application-on-device-boot – TheMisir Oct 29 '20 at 08:31
-
thanks. it works perfectly for me. is it possible to get a sent file link? – hassan moradnezhad Nov 14 '20 at 11:42
-
@hassanmoradnezhad I'm not sure about that. But you can try decrypting whatsapp database (https://stackoverflow.com/questions/8856874/get-all-messages-from-whatsapp). I'm not sure if this is legit or not but it might help to achieve what you're looking for. – TheMisir Nov 14 '20 at 14:06
Unless Developers of that app intentionally share a service , a content provider, or intentionally send out public broadcasts of events, or expose a custom broadcast registering system, there is no legitimate way in android to listen to internal workings of a third party App. App isolation is designed in Android for a very important reason: security.

- 29,290
- 3
- 79
- 130
-
Well, I was able to do this using Accessibility Service. Check my answer. Thanks. – sanjeev mk Feb 16 '13 at 06:53
Accessibility events only catch incoming notifications event, not when they are updated. For now, WhatsApp notifications don't display the message, only the sender. The message is then added by the WhatsApp app with an update, which can't be caught by the accessibility service.
You will only have something like "1 new message from XXX", but that may be sufficient to your needs.

- 93
- 1
- 8
-
no .. if u recieve one message , then it will show that message .. if u receive 2 or more messages .. then it will show as u said – pola sai ram Jul 07 '15 at 09:55
-
-
is there a way to read all messages from WhatsApp notification? – subrahmanya bhat Feb 22 '17 at 19:11
-
1
-
1@RizwanAhmed yes you can. Whenever a message is delivered to your phone a meta is also sent. All of the information is contained in that meta including the phone number and the sender name. You can use a broadcast receiver to fetch that broadcast. – Han Nov 22 '17 at 09:28
-
@Han and how do we retrieve that meta. I mean which type of broadcast receiver do i have to register. Can you please explain a little more. – Deepak Vajpayee Jun 24 '20 at 08:08
See below example to catch whatsapp notifications:
public class Notifier extends AccessibilityService {
@Override
public void onCreate(){
//Toast.makeText(this,"Oncreate", Toast.LENGTH_LONG).show();
}
@Override
protected void onServiceConnected() {
// Set the type of events that this service wants to listen to. Others
// won't be passed to this service.
Toast.makeText(this,"Service connected", Toast.LENGTH_LONG).show();
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;;
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED ;
// If you only want this service to work with specific applications, set their
// package names here. Otherwise, when the service is activated, it will listen
// to events from all applications.
info.packageNames = new String[] {"com.whatsapp"};
info.notificationTimeout = 100;
setServiceInfo(info);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if(event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
Toast.makeText(this,"Notification Catched", Toast.LENGTH_LONG).show();
}
}
}
And don't forget to set permission from settings>Accessibility in order to access the system events. Allow permission from settings .
check this link

- 441
- 5
- 6