Programmatically click on Android notification
Asked Answered
E

3

7

I am black-box testing an application by using the Robotium framework. The app under test send a notification in the status bar each time I install a new application. I would like to click on that notification, but I still did not find a proper way to do it.

When I manually click on one notification I get this logcat lines:

I/ActivityManager(  148): START {flg=0x14000000 cmp=com.test.package/.activity.FrontActivity bnds=[0,38][240,86] (has extras) u=0} from pid -1
I/ActivityManager(  148): START {flg=0x14000000 cmp=com.test.package/.activity.ResultActivity u=0} from pid 8600
I/ActivityManager(  148): Displayed com.test.package/.activity.FrontActivity: +1s183ms
I/ActivityManager(  148): Displayed com.test.package/.activity.ResultActivity: +744ms

I know Robotium cannot test 2 different applications at the same time, as well as I know you can't get the notification of an external application.

I also tried to get the pending intent and fire it up by using the following code:

Context context = this.getInstrumentation().getTargetContext().getApplicationContext();
String intentClassString = "com.test.package.activity.FrontActivity";
Class<?> intentClass = null;

try {
    intentClass = Class.forName(intentClassString);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

Intent intent = new Intent(context, intentClass);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_NO_CREATE);

try {
    pendingIntent.send();
} catch (CanceledException e) {
    e.printStackTrace();
}

The activity is actually showed, but it is different from what I obtain by manually invoking it. I think something is wrong in the context I pass, or the way I call the pending intent.

Any hint about this issue? Is there a better way to simulate the click on a notification?

Esau answered 16/12, 2012 at 19:42 Comment(0)
S
1

You can trigger the swipe on notification bar and tap event on notification.

Refer commands :

adb shell input swipe 100 500 400 100 1000

adb shell input tap 400 400

by changing the arguments you can use these commands.

Stettin answered 29/12, 2016 at 6:11 Comment(0)
R
0

Please find good dicussion and a few options in this other stackoverflow question:
Can I test status bar notifications using Android's testing framework?

Since you already have code to fire up an intent, instead of trying to recreate what the notify() call would do, try adding a special activity just for testing that actually calls notify() on the notification.

Rutherfordium answered 18/8, 2013 at 16:39 Comment(0)
S
0

The answer that Ranjith KP gave you contains the solution. Be aware that you need the phone who is running the app rooted, as these are commands used with sudo priviledges.

This is the code that is working for me. It first extends the notification bar (swipe command), waits 1 second and finally taps the first notification on the list. Modify the axis parameters as you wish.

Process su = null;
try {
 su = Runtime.getRuntime().exec("su");
 su.getOutputStream().write("input swipe 270 010 270 900\n".getBytes());
 Thread.sleep(1000);
 su.getOutputStream().write("input tap 200 200\n".getBytes());
 su.getOutputStream().write("exit\n".getBytes());
 su.waitFor();
} catch (Exception e) {
 e.printStackTrace();
} finally {
 if (su != null) {
  su.destroy();
 }
}
Sphene answered 2/1, 2017 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.