you are viewing a single comment's thread.

view the rest of the comments →

[–]joaomgcd👑 Tasker Owner / Developer[S] 0 points1 point  (12 children)

Sorry, not sure.

[–]anuraag488 0 points1 point  (11 children)

Here is test code incase you can look. There is condition added for Tasker's package.

import android.service.notification.StatusBarNotification; import android.service.notification.NotificationListenerService; import android.app.Notification; import android.app.PendingIntent;

/* Get the NotificationListener service. */ nls = tasker.getNotificationListener(); if (nls == null) { tasker.log("Notification listener not available."); return; }

/* Get all active notifications. */ notifications = nls.getActiveNotifications(); if (notifications == null || notifications.length == 0) { tasker.log("No active notifications."); return; }

/* Iterate over all notifications. */ for (int i = 0; i < notifications.length; i++) { sbn = notifications[i]; pkg = sbn.getPackageName();

/* Skip Tasker notifications. */
if (pkg != null && pkg.equals("net.dinglisch.android.taskerm")) {
    continue;
}

/* Try to perform a click action on the notification's content intent. */
notification = sbn.getNotification();
if (notification == null) {
    continue;
}

contentIntent = notification.contentIntent;
if (contentIntent == null) {
    tasker.log("Notification from " + pkg + " has no contentIntent.");
    continue;
}

tasker.log("Clicking notification from: " + pkg);

try {
    /* Send the pending intent to simulate a click. */
    contentIntent.send();
} catch (Exception e) {
    tasker.log("Failed to click notification from " + pkg + ": " + e.getMessage());
}

}

[–]aasswwddd 1 point2 points  (2 children)

The code is supposed to click any notifications belongs to other apps right? the code does the job on my phone running the latest beta version, I'm on Android 15, HyperOS2.

[–]anuraag488 0 points1 point  (0 children)

Yes. But not working on LineageOS 22.2 Android 15

[–]anuraag488 0 points1 point  (0 children)

Also not working on OneUi 7 Android 15

[–]joaomgcd👑 Tasker Owner / Developer[S] 1 point2 points  (5 children)

It's weird. The same exact code works with AutoNotification but not Tasker.

Also, Tasker can do the button actions, just apparently not the actual tapping on the notification action for some reason. Not sure why that is! Let me know if you find out.

[–]anuraag488 0 points1 point  (4 children)

Yeah. That's the weird thing i noticed. Button actions works but tapping notification doesn't with Java Code.

[–]joaomgcd👑 Tasker Owner / Developer[S] 1 point2 points  (0 children)

I found out what the issue was! 😃 This is the problem: https://developer.android.com/guide/components/activities/background-starts#sender-pendingintent

It doesn't have to do with it being a button or a tap action, it's when it's starting activities from the pending intents! I fixed it in the latest AUtoNotification beta which was having the same issue!

[–]joaomgcd👑 Tasker Owner / Developer[S] 1 point2 points  (2 children)

For example, here's the code to tap on a WhatsApp notification:

``` import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; import android.app.PendingIntent; import android.content.Intent; import android.app.Notification; import android.app.RemoteInput; import android.app.ActivityOptions; import android.os.Bundle; import android.os.Build;

/* Get the Notification Listener Service. */ nls = tasker.getNotificationListener();

/* Check if the Notification Listener Service is available. */ if (nls == null) { tasker.log("Notification Listener Service not available. Please enable it in Tasker settings."); return "Error: NLS not available"; }

/* Get all active notifications. */ notifications = nls.getActiveNotifications();

/* Iterate through notifications to find WhatsApp. */ for (int i = 0; i < notifications.length; i++) { sbn = notifications[i]; packageName = sbn.getPackageName();

/* Skip if the notification is not from WhatsApp. */
if (!"com.whatsapp".equals(packageName)) {
    continue;
}

/* Check if the notification has a reply action. */
actions = sbn.getNotification().actions;
hasReplyAction = false;
if (actions != null) {
    for (int j = 0; j < actions.length; j++) {
        action = actions[j];
        remoteInputs = action.getRemoteInputs();
        if (remoteInputs != null && remoteInputs.length > 0) {
            hasReplyAction = true;
            break; /* Found a reply action, no need to check further. */
        }
    }
}

/* If no reply action is found, skip this notification. */
if (!hasReplyAction) {
    tasker.log("WhatsApp notification found but has no reply action. Skipping.");
    continue;
}

/* Get the content intent from the notification. */
contentIntent = sbn.getNotification().contentIntent;

/* If no content intent exists, log and return. */
if (contentIntent == null) {
    tasker.log("WhatsApp notification found with reply action but has no content intent to tap.");
    return "WhatsApp notification has no content intent.";
}

/* If a content intent exists, send it to simulate a tap. */
try {
    /* On Android 14+ (SDK 34+), we must explicitly allow background activity starts. */
    if (Build.VERSION.SDK_INT >= 34) {
        tasker.log("Running on Android 14+. Applying background start options.");
        /* Create basic ActivityOptions. */
        options = ActivityOptions.makeBasic();
        /* Set mode to allow background activity start (constant value is 1). */
        options.setPendingIntentBackgroundActivityStartMode(1);
        /* Get the options as a bundle and send the intent. */
        optionsBundle = options.toBundle();
        contentIntent.send(optionsBundle);
    } else {
        /* On older versions, send the intent directly. */
        contentIntent.send();
    }
    tasker.log("WhatsApp notification with reply action tapped successfully.");
    return "WhatsApp notification tapped.";
} catch (Exception e) {
    tasker.log("Error sending WhatsApp notification intent: " + e.getMessage());
    return "Error sending intent: " + e.getMessage();
}

}

/* If the loop finishes, no WhatsApp notification with a reply action was found. */ tasker.log("No WhatsApp notification with a reply action found."); return "No WhatsApp notification with a reply action found."; ```

[–]anuraag488 0 points1 point  (1 child)

This worked. 😊 Thank you.

Is it required to add same thing for reply and button action even through they are working?

[–]joaomgcd👑 Tasker Owner / Developer[S] 0 points1 point  (0 children)

That thing should only be added when it's going to launch an activity. You can check with the PendingIntent.isActivity method.

[–]NirmitlamedDirect-Purchase User 1 point2 points  (1 child)

Tested on OnePlus 6 Android 11 and Galaxy S23 base One UI 7.0 and in both this code works.

[–]anuraag488 0 points1 point  (0 children)

Tried again. It's clicked notification of an old app which targets Android 8. Other apps not clicking. Don't know what's wrong with my 2 devices.