Android Question Notification Reply

Fusseldieb

Active Member
Licensed User
Longtime User
Hi,
actually it's possible to reply to notifications since some time ago and some apps make use of it (See "AutoResponder for WA").
I want to implement such behaviour in my app to respond to chats/stuff that I receive, but I have no idea where to start, or if there's any B4A library for it.

Thanks in advance.
 

DonManfred

Expert
Licensed User
Longtime User
Whatsapp does not provide an Api to do this.
Telegram does provide a Api but i dont know if there is a B4A Lib for it. For B4J there is one.
 
Upvote 0

Fusseldieb

Active Member
Licensed User
Longtime User
Whatsapp does not provide an Api to do this.
Telegram does provide a Api but i dont know if there is a B4A Lib for it. For B4J there is one.
Thanks for the reply.
I know that WhatsApp doesn't provide an API to do that for obvious reasons, but some apps actually can RESPOND to incoming notifications. I don't know if they exploit the Android Wear capability to catch notifications and respond to them or if we simply can reply to notifications with the NotificationListener or such.

You know that reply button in the notifications itself, do you? I can reply to some chat directly from there, and I think, some apps may access that field and respond to chats.
 
Upvote 0

Fusseldieb

Active Member
Licensed User
Longtime User
Exactly, and some apps do access that field and respond to incoming text from specific app packages (somehow).
My question is how do they do that, and, as a "bonus point", how can I do that with B4A?

EDIT: After long digging, I found the site again where I found the "Java" version of the code:
https://medium.com/@polidea/how-to-respond-to-any-messaging-notification-on-android-7befa483e2d7
If 'exploits' the Android Wear capability ("WearableExtender").

EDIT2: Quick glimpse at the page (Posting directly here in case the site ever goes offline) :
One line to get WearableExtender, how cool would that be?
B4X:
WearableExtender wearableExtender = new WearableExtender(statusBarNotification.getNotification());

Ok, so now lets just extract Actions and from them our beloved RemoteInput.
B4X:
List<Action> actions = wearableExtender.getActions();
for(Action act : actions) {
if(act != null && act.getRemoteInputs() != null) {
RemoteInput[] remoteInputs = act.getRemoteInputs();
}
}

Ok, but what else do we need? We definitely need PendingIntent to send our results back to the app that triggered the notification and it would also be good to keep the Bundle as it might contain some extra values such as conversationId. Let’s do it!
B4X:
notificationWear.pendingIntent = statusBarNotification.getNotification().contentIntent;
notificationWear.bundle = statusBarNotification.getNotification().extras;

Now we are ready to send it back! The approach in our sample app, to speed up the development process of the POC, was to pass our data via EventBus to Activity where it would be saved on Stack, and when the user clicks “Respond to last” to just pop the last item and put fake response text into it.

Filling the RemoteInput
Probably the most interesting part is how we can fill an object that normally takes voice input with our fake text. It is not that hard.
B4X:
RemoteInput[] remoteInputs = new RemoteInput[notificationWear.remoteInputs.size()];

Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle localBundle = notificationWear.bundle;
int i = 0;
for(RemoteInput remoteIn : notificationWear.remoteInputs){
    getDetailsOfNotification(remoteIn);
    remoteInputs[i] = remoteIn;
    localBundle.putCharSequence(remoteInputs[i].getResultKey(), "Our answer");//This work, apart from Hangouts as probably they need additional parameter (notification_tag?)
    i++;
}
RemoteInput.addResultsToIntent(remoteInputs, localIntent, localBundle);
try {
    notificationWear.pendingIntent.send(MainActivity.this, 0, localIntent);
} catch (PendingIntent.CanceledException e) {
    Log.e(TAG, "replyToLastNotification error: " + e.getLocalizedMessage());
}
Our notificationWear object is a temporary container for all needed data to respond to a particular notification. Then we take each RemoteInput from the saved notification and fill it with our desired text by using the putCharSequence() method on a Bundle that we will pass back. The key here is to use getResultKey() on each RemoteInput as this is where the called app will look for the reply text in the returned Bundle. As a final step we need to populate Intent with RemoteInputs that we just filled with fake reply, to do so let’s use addResultsToIntent(inputs, intent, bundle). Now we are ready to fire PendingIntent with all that data, to trigger it let’s call pendingIntent.send(context, code,intent). This is all, we just responded to a conversation in Messenger, Telegraph, Line or any other app that uses WearableExtender with RemoteInput! This method still lacks support for Hangouts as probably they require to pass some additional parameter — our best guess is Tag of StatusBarNotification as in case of other (working) apps it is null.

EDIT3: I think it's not that hard since "WearableExtender" shows up in the B4X search, which probably is a good sign: https://www.b4x.com/android/forum/pages/results/?query=wearable+extender
 
Last edited:
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
The link I posted and the THings you posted aren't exactly the same thing. I started updating the NotificationBuilder lib a couple of months ago and it included the method i linked to, but this is only good for newer versions of android. The update was bigger than the time i had to spend on it, hoping to get back to it soon. You could always make a library yourself ;)
 
Upvote 0

Fusseldieb

Active Member
Licensed User
Longtime User
The link I posted and the THings you posted aren't exactly the same thing. I started updating the NotificationBuilder lib a couple of months ago and it included the method i linked to, but this is only good for newer versions of android. The update was bigger than the time i had to spend on it, hoping to get back to it soon. You could always make a library yourself ;)
Yes, I understood, but have you any knoweledge of any B4A library that has that WearableExtender capability to do that what I linked above?

PS: I tried to write some libraries in the past and I failed MISERABLY.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
The NotificationBuilder library has WearExtender capabilities AND Actions. It just doesn't do things in the specific way outlined above.
 
Upvote 0
Top