Since I am also working on an IOT product, this was one of the biggest problems I encountered, but after some research, I believe that I found some solution to this problem, or you can say a simple hack. I tested this hack on multiple devices with multiple versions and found that most devices respond. Only Samsung devices are not responding, some Huawei devices and some Oppo devices are also not responding (I'm still watching something for these devices too).
I noticed that Android provides one of the "Notification Access" features. You can use NotificationListenerService to read notifications and perform some actions on them. It provides some override methods:
onNotificationPosted() onNotificationRemoved() getActiveNotifications()
... etc.
Here is the code: Create a service that extends NotificationListenerService
class NLService extends NotificationListenerService { @Override public void onNotificationPosted(StatusBarNotification sbn) { .... } @Override public void onNotificationRemoved(StatusBarNotification sbn) { .... }
In AndroidMenifest add this service as:
<service android:name=".NLService" android:label="@string/app_name" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service>
This will allow your application to read the notifications received.
Now, here is the main code:
In onNotificationPosted (StatusBarNotification sbn) add this code:
@Override public void onNotificationPosted(StatusBarNotification sbn) { try { if (sbn.getNotification().actions != null) { for (Notification.Action action : sbn.getNotification().actions) { Log.e(TAG, "" + action.title); if (action.title.toString().equalsIgnoreCase("Answer")) { Log.e(TAG, "" + true); PendingIntent intent = action.actionIntent; try { intent.send(); } catch (PendingIntent.CanceledException e) { e.printStackTrace(); } } } } } catch (Exception e) { e.printStackTrace(); } }
Here it is!
Everything is installed. Launch the application and devices, with the exception of Samsung, depending on what displays an incoming call notification with the Answer and Reject / Cancel buttons, you can answer the call.
To open the Notification Access Settings and let your application read the notification, use:
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); startActivity(intent);
Just create a POC for this and let me know how it works.
Please mark my answer if this helps.
In addition, if you can provide some solution on this subject regarding Samsung devices, please update.
thank