How I can replay to WhatsApp notification from java code in android studio

Issue

Hello guys I’m using NotificationListenerService to get WhatsApp messages

But now I have a problem that I don’t know how to replay to WhatsApp notifications
So here is the code and how I’m getting WhatsApp notifications but my problem is in how actually to replay on them

public class NotificationListener extends NotificationListenerService {

// StaticFields:
public static final String WHATSAPP_PACKAGE_NAME = "com.whatsapp";
// TAGS:
private static final String TAG = "NotificationListener";
private static final String NP_FIELD = "onNotificationPosted: ";

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    super.onNotificationPosted(sbn);
    // Fields:
    ARPreferencesManager manager = new ARPreferencesManager(getApplicationContext());
    String currentPackages = manager.getStringPreferences(ARPreferencesManager.PACKAGE_APP_NAME);
    List<Chat.Messages> messages = new ArrayList<>();
    // CheckingStatusBarNotification:
    if (sbn.getPackageName().equals(WHATSAPP_PACKAGE_NAME)) {
        // Initializing(DateTime):
        String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date());
        List<Chat> chats;
        // Initializing(Data):
        String sender = sbn.getNotification().extras.getString(Notification.EXTRA_TITLE);
        String msg = sbn.getNotification().extras.getString(Notification.EXTRA_TEXT);
        String currentSenders = manager.getStringPreferences(ARPreferencesManager.SENDER_NAME);
        // AddingData:
        messages.add(new Chat.Messages(msg));
        // Developing:
        if (manager.getPreferences().contains(ARPreferencesManager.WHATSAPP_CHATS)) {
            // Initializing:
            chats = ARUtils.fromJsonToChats(manager.getStringPreferences(ARPreferencesManager.WHATSAPP_CHATS));
            // Developing:
            for (Chat chat : chats) {
                if (chat.getSender().equals(sender)) {
                    // AddingTheNewMessage:
                    chat.getMessages().addAll(messages);
                    // AddingSender($Preferences):
                    if (!currentSenders.contains(sender)) {
                        // Initializing:
                        manager.setStringPreferences(ARPreferencesManager.SENDER_NAME, sender + ",");
                        // Refreshing:
                        currentSenders = manager.getStringPreferences(ARPreferencesManager.SENDER_NAME);
                    }
                }
            }
            // CheckingSenders:
            if (!currentSenders.contains(sender)) {
                // Initializing:
                Chat chat = new Chat(sender, "", date, null, messages);
                // AddingTheNewChat:
                chats.add(chat);
            }
        } else {
            // Initializing:
            chats = new ArrayList<>();
            // Developing:
            chats.add(new Chat(sender, "", date, null, messages));
        }
        // SettingPreferences:
        manager.setStringPreferences(ARPreferencesManager.WHATSAPP_CHATS, ARUtils.fromChatsToJson(chats));
        // Debugging:
        ARUtils.debug(TAG, NP_FIELD, manager.getStringPreferences(ARPreferencesManager.WHATSAPP_CHATS));
    }
    // Debugging:
    ARUtils.debug(TAG, NP_FIELD, "Whatsapp Package Was Founded In Preferences");
    // Debugging:
    ARUtils.debug(TAG, NP_FIELD, "Start");
}

}

Solution

Did you try using this function?

fun reply(action: Notification.Action, message: String, exception: Exception.() -> Unit = {}) {
    try {
        val sendIntent = Intent()
        val msg = Bundle()
        for (inputable in action.remoteInputs) {
            msg.putCharSequence(inputable.resultKey, message)
        }
        RemoteInput.addResultsToIntent(action.remoteInputs, sendIntent, msg)
        action.actionIntent.send(applicationContext, 0, sendIntent)
    } catch (exception: Exception) {
        exception(exception)
    }
}

You can get Notification.Action for this way:

override fun onNotificationPosted(sbn: StatusBarNotification) {
    super.onNotificationPosted(sbn)
    val wExt = Notification.WearableExtender(sbn.notification)
    for (action in wExt.actions) {
        ...

Answered By – Ji Sungbin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published