Manoj j
Manoj j

Reputation: 152

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.

Upvotes: 6

Views: 10541

Answers (6)

fbiego
fbiego

Reputation: 418

On Android 7+, get the notification using NotificationServiceListener() then check if it has RemoteInput(). Finally try to send the reply on the RemoteInput

Here is a sample app

In the NotificationService, the notification is replied if it matches the checks

The notification access permission must be enabled to receive the notifications

Upvotes: 0

Sreenath S
Sreenath S

Reputation: 11

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.

Upvotes: 0

Nil
Nil

Reputation: 274

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.

Upvotes: 10

Mukesh Gupta
Mukesh Gupta

Reputation: 1433

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

Upvotes: 0

Xut
Xut

Reputation: 33

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");

Upvotes: 0

webo80
webo80

Reputation: 3403

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

Upvotes: 4

Related Questions