├── .gitignore
├── .idea
├── .name
├── codeStyles
│ └── Project.xml
├── gradle.xml
├── jarRepositories.xml
├── misc.xml
├── runConfigurations.xml
└── vcs.xml
├── .project
├── .settings
└── org.eclipse.buildship.core.prefs
├── README.md
├── app
├── .classpath
├── .gitignore
├── .project
├── .settings
│ └── org.eclipse.buildship.core.prefs
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── dh
│ │ └── notice
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── dh
│ │ │ └── notice
│ │ │ ├── HelperNotificationListenerService.java
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── dh
│ └── notice
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | notice
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | xmlns:android
14 |
15 | ^$
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | xmlns:.*
25 |
26 | ^$
27 |
28 |
29 | BY_NAME
30 |
31 |
32 |
33 |
34 |
35 |
36 | .*:id
37 |
38 | http://schemas.android.com/apk/res/android
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | .*:name
48 |
49 | http://schemas.android.com/apk/res/android
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | name
59 |
60 | ^$
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | style
70 |
71 | ^$
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | .*
81 |
82 | ^$
83 |
84 |
85 | BY_NAME
86 |
87 |
88 |
89 |
90 |
91 |
92 | .*
93 |
94 | http://schemas.android.com/apk/res/android
95 |
96 |
97 | ANDROID_ATTRIBUTE_ORDER
98 |
99 |
100 |
101 |
102 |
103 |
104 | .*
105 |
106 | .*
107 |
108 |
109 | BY_NAME
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
20 |
21 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | notice
4 | Project notice created by Buildship.
5 |
6 |
7 |
8 |
9 | org.eclipse.buildship.core.gradleprojectbuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.buildship.core.gradleprojectnature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.buildship.core.prefs:
--------------------------------------------------------------------------------
1 | arguments=
2 | auto.sync=false
3 | build.scans.enabled=false
4 | connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
5 | connection.project.dir=
6 | eclipse.preferences.version=1
7 | gradle.user.home=
8 | java.home=C\:/Program Files/Java/jdk1.8.0_251
9 | jvm.arguments=
10 | offline.mode=false
11 | override.workspace.settings=true
12 | show.console.view=true
13 | show.executions.view=true
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # app-notification-listener
2 | 监听安卓任意app通知栏消息内容,并将内容发送到服务器数据库
3 |
4 | ## 背景
5 | 监听消息通知用处很多,比如大家乐见的监听微信/支付宝到账通知实现支付回调。经过几天折腾,写了这个demo, 打通了从APP(安卓)获取**任意**消息通知并发送到服务端(PHP+MySql)的完整流程。
6 |
7 | ## 实现思路
8 | 继承安卓消息类`NotificationListenerService`,在调用`onListenerConnected`、`onNotificationPosted`、`onNotificationRemoved`等方法时执行监听逻辑。
9 |
10 | ## 效果预览
11 |
12 | #### APP演示动画(.gif ~3Mb):
13 |
14 |
15 | #### 后台界面:
16 | ![app-notice-admin.png][2]
17 |
18 | ## Demo地址
19 | #### apk安装包:[http://denghao.me/special/appNotice/appNotice.apk][5] (debug版,某些安卓版本可能运行不了)
20 | #### 后台地址 [https://denghao.me/special/appNotice/list.php][4]
21 |
22 | ## 提醒
23 | - Android非我专业, 这个demo是自己参考各方资料后拼凑出来的,仅供学习使用。
24 | - 跑起APP需要安装一堆软件和依赖包,请作好心理准备。
25 |
26 | ## 联系我
27 | - [https://denghao.me][3]
28 | - Email: jie4038[at]qq.com
29 |
30 |
31 | [1]: https://denghao.me/usr/uploads/2020/06/1599131392.jpg
32 | [2]: https://denghao.me/usr/uploads/2020/06/831407762.png
33 | [3]: https://denghao.me
34 | [4]: https://denghao.me/special/appNotice/list.php
35 | [5]: ![https://denghao.me/special/appNotice/appNotice.apk]
36 |
--------------------------------------------------------------------------------
/app/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | app
4 | Project app created by Buildship.
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.buildship.core.gradleprojectbuilder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 | org.eclipse.buildship.core.gradleprojectnature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/.settings/org.eclipse.buildship.core.prefs:
--------------------------------------------------------------------------------
1 | arguments=
2 | auto.sync=false
3 | build.scans.enabled=false
4 | connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(6.3))
5 | connection.project.dir=..
6 | eclipse.preferences.version=1
7 | gradle.user.home=
8 | java.home=C\:/Program Files/Java/jdk1.8.0_251
9 | jvm.arguments=
10 | offline.mode=false
11 | override.workspace.settings=true
12 | show.console.view=true
13 | show.executions.view=true
14 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 29
5 | buildToolsVersion "29.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.dh.notice"
9 | minSdkVersion 22
10 | targetSdkVersion 29
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15 | }
16 |
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: "libs", include: ["*.jar"])
27 | implementation 'androidx.appcompat:appcompat:1.1.0'
28 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
29 | testImplementation 'junit:junit:4.12'
30 | androidTestImplementation 'androidx.test.ext:junit:1.1.1'
31 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
32 |
33 | implementation "com.squareup.okhttp3:okhttp:3.10.0"
34 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/dh/notice/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.dh.notice;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 | assertEquals("com.dh.notice", appContext.getPackageName());
25 | }
26 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dh/notice/HelperNotificationListenerService.java:
--------------------------------------------------------------------------------
1 | package com.dh.notice;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.app.Notification;
5 | import android.content.Intent;
6 | import android.service.notification.NotificationListenerService;
7 | import android.service.notification.StatusBarNotification;
8 | import android.util.Log;
9 | import android.widget.RemoteViews;
10 | import android.os.Bundle;
11 | import android.os.Build;
12 | import java.lang.reflect.Field;
13 | import java.util.ArrayList;
14 | import java.util.HashMap;
15 | import java.util.Map;
16 |
17 | public class HelperNotificationListenerService extends NotificationListenerService {
18 |
19 | public static String SEND_MSG_BROADCAST = "notify_msg";
20 |
21 | @Override
22 | public void onListenerConnected() {
23 | Intent intent = new Intent();
24 | intent.putExtra("msg", "开始监听");
25 | intent.setAction(SEND_MSG_BROADCAST);
26 | sendBroadcast(intent);
27 | System.out.println("onListenerConnected 成功联接...");
28 |
29 | }
30 |
31 | // 在收到消息时触发
32 | @Override
33 | public void onNotificationPosted(StatusBarNotification sbn,RankingMap rankingMap) {
34 | // 获取接收消息APP的包名
35 | String packageName = sbn.getPackageName();
36 | String content = null;
37 |
38 | // 当 API > 18 时,使用 extras 获取通知的详细信息
39 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
40 | Bundle extras = sbn.getNotification().extras;
41 | if (extras != null) {
42 | // 获取通知标题
43 | String title = extras.getString(Notification.EXTRA_TITLE, "");
44 | // 获取通知内容
45 | String text = extras.getString(Notification.EXTRA_TEXT, "");
46 | content = packageName + "---" + title + "---" + text;
47 | }
48 | } else {
49 | Map notiInfo = getNotiInfo(sbn.getNotification());
50 | if (null != notiInfo) {
51 | content =packageName + "---" + notiInfo.get("title") + "---" + notiInfo.get("text");
52 | }
53 | }
54 | if (content == null || content.length() == 1) {
55 | return;
56 | }
57 | Intent intent = new Intent();
58 | intent.putExtra("msg", content);
59 | intent.setAction(SEND_MSG_BROADCAST);
60 | sendBroadcast(intent);
61 | }
62 |
63 | private Map getNotiInfo(Notification notification) {
64 | int key = 0;
65 | if (notification == null)
66 | return null;
67 | RemoteViews views = notification.contentView;
68 | if (views == null)
69 | return null;
70 | Class secretClass = views.getClass();
71 |
72 | try {
73 | Map text = new HashMap<>();
74 |
75 | Field outerFields[] = secretClass.getDeclaredFields();
76 | for (int i = 0; i < outerFields.length; i++) {
77 | if (!outerFields[i].getName().equals("mActions"))
78 | continue;
79 |
80 | outerFields[i].setAccessible(true);
81 |
82 | ArrayList