'Automatic reply for whatsapp messages approach

I have seen few applications on play store that supports automatic reply for WhatsApp, I searched the internet to find out the approach, but all I found was this piece of code

    Uri uri = Uri.parse("smsto:" + "99********");
    Intent i = new Intent(Intent.ACTION_SENDTO, uri);
    i.putExtra("sms_body", "Hey!");
    i.setPackage("com.whatsapp");
    startActivity(i);

It will open the WhatsApp and take you to that particular contact if you have saved and it will paste the given text but it will not send the message.

links

https://play.google.com/store/apps/details?id=horizontstack.autoreplyforwhatsapp.whatsreply

They are accessing the notifications to get the messages. I want to know how they are sending the messages in the background without opening the application. If somebody knows the approach please share here.



Solution 1:[1]

I did by this:

Step 1: copy all code from NotificationHelperLibrary repository.
Step 2: Create Notification Listener Service and put below code in onNotificationPosted(..) method:

MyNotifiService.this.cancelNotification(sbn.getKey());
Action action = NotificationUtils.getQuickReplyAction(sbn.getNotification(), getPackageName());

if (action != null) {
    Log.i(TAG, "success");
    try {
        action.sendReply(getApplicationContext(), "Hello");
    } catch (PendingIntent.CanceledException e) {
        Log.i(TAG, "CRAP " + e.toString());
    }
} else {
    Log.i(TAG, "not success");
}

This is a basic demo.

Solution 2:[2]

In newer versions of WhatsApp and Android OS, you can reply directly from the notification, this is how the apps are doing that. Probably nothing to do with the code you posted. So, if you want to implement auto-reply, you must deal with the notification, and keep in mind the Android OS version limitation

EDIT: Check this post to read notifications using accesibility services

Solution 3:[3]

Whatsapp messages contain the Car Extension. You can use it to get the corresponding PendingIntent that you will need to reply to the message.

Bundle extension = NotificationCompat.getExtras(notification).getBundle("android.car.EXTENSIONS");
Bundle conversation = extension.getBundle("car_conversation");
PendingIntent reply = conversation.getParcelable("on_reply");

Solution 4:[4]

In newer version(>= Android 7 ), we have the ability to reply to notification directly from notificationlistener(i have tested it works for all messaging app with multiple messages from user)

This link will be useful for this=> link

For earlier version, we can get action for notification from wearable notification. See this for more info :- link

Source code for above post :- link

Solution 5:[5]

WhatsApp provides support for android wearables like smart watches. Users can send their reply through these kind of gadgets. So those apps collects the notification by pretending to be a smart wearable and they sends their without even opening the WhatsApp.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TheMisir
Solution 2
Solution 3 Xut
Solution 4 Mukesh Gupta
Solution 5 Sreenath S