6

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.

Salman Khalid
  • 543
  • 5
  • 23
Manoj j
  • 152
  • 4
  • 15

6 Answers6

10

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.

TheMisir
  • 4,083
  • 1
  • 27
  • 37
Nil
  • 274
  • 3
  • 11
  • How to get if there are reply action on the notification? this code works but the Action Object always returns null...even if the action can be used to reply to messages... – LegendSayantan Dec 26 '21 at 16:40
4

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

webo80
  • 3,365
  • 5
  • 35
  • 52
0

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");
Xut
  • 23
  • 4
0

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

Mukesh Gupta
  • 1,373
  • 3
  • 17
  • 42
0

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.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 18 '22 at 08:42
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31314284) – ThunderStruct Mar 20 '22 at 23:56
0

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

fbiego
  • 328
  • 3
  • 12