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"?
Enable Notification Sound and Vibration To check or turn on sound and vibration for your notifications: Tap on the three dots at the top right (as soon as you open WhatsApp) and go to Settings. Tap on Notifications. Toggle on Conversation tones.
Make sure Do not disturb is turned off or you have allowed WhatsApp notifications in priority mode in your phone's Settings app > Sound > Do not disturb. Make sure all of WhatsApp's permissions are granted in your phone's Settings app > Apps > WhatsApp > Permissions.
To change which of your notifications gets read out loud, tab on the settings wheel in the right top corner of the homescreen, then tap on 'Read other messages & notifications' and choose WhatsApp.
You can use NotificationListenerService to listen for notifications and get contents. 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.
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.
I have to use NotificationListenerService
to get detailed info about notification.
Here's service code 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); } }
And the code required to be placed in 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, a special permission is required. Here's the code which you can use to open settings page where user have to allow the app to listen to incoming notifications. I placed this snippet in MainActivity onCreate
function.
startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS))
If you need to check whether or not the notification listener permission is granted using the code below:
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; }
PS: I don't know or can not explain why/how it works because I copied the codes from my 1 year old project.
I listened to incoming WhatsApp message notifications with the help of this 2 parted article which you can read it from here.
<!-- 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>
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" />
MyAccessibilityService
class which extends AccessibilityService
.@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 } } } }
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
To send replies to received notifications check out this answer.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With