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 ho
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:
To intercept notifications, a special Permission is required. You can open notification listener settings using the code bellow, I placed this code inside my MainActivity.
startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS))
And lastly, you can check if permission was given before or not using the code bellow:
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.
serviceconfig.xml in /xml/ directory.
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.