├── .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 | 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 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 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 actions = (ArrayList) outerFields[i].get(views); 83 | for (Object action : actions) { 84 | Field innerFields[] = action.getClass().getDeclaredFields(); 85 | Object value = null; 86 | Integer type = null; 87 | for (Field field : innerFields) { 88 | field.setAccessible(true); 89 | if (field.getName().equals("value")) { 90 | value = field.get(action); 91 | } else if (field.getName().equals("type")) { 92 | type = field.getInt(action); 93 | } 94 | } 95 | // 经验所得 type 等于9 10为短信title和内容,不排除其他厂商拿不到的情况 96 | if (type != null && (type == 9 || type == 10)) { 97 | if (key == 0) { 98 | text.put("title", value != null ? value.toString() : ""); 99 | } else if (key == 1) { 100 | text.put("text", value != null ? value.toString() : ""); 101 | } else { 102 | text.put(Integer.toString(key), value != null ? value.toString() : null); 103 | } 104 | key++; 105 | } 106 | } 107 | key = 0; 108 | 109 | } 110 | return text; 111 | } catch (Exception e) { 112 | e.printStackTrace(); 113 | } 114 | return null; 115 | } 116 | 117 | // 118 | // // 在删除消息时触发 119 | // @Override 120 | // public void onNotificationRemoved(StatusBarNotification sbn) { 121 | // Bundle extras = sbn.getNotification().extras; 122 | // // 获取接收消息APP的包名 123 | // String notificationPkg = sbn.getPackageName(); 124 | // // 获取接收消息的抬头 125 | // String notificationTitle = extras.getString(Notification.EXTRA_TITLE); 126 | // // 获取接收消息的内容 127 | // String notificationText = extras.getString(Notification.EXTRA_TEXT); 128 | // System.out.println("移除消息: "+notificationPkg +" & "+ notificationTitle + " & " + notificationText); 129 | // } 130 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dh/notice/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.dh.notice; 2 | import androidx.appcompat.app.AppCompatActivity; 3 | import android.app.Activity; 4 | import android.content.BroadcastReceiver; 5 | import android.content.ComponentName; 6 | import android.content.Context; 7 | import android.content.Intent; 8 | import android.content.IntentFilter; 9 | import android.content.pm.PackageManager; 10 | import android.os.Build; 11 | import android.util.Log; 12 | import android.provider.Settings; 13 | import android.os.Bundle; 14 | import android.text.TextUtils; 15 | import android.view.View; 16 | import android.widget.TextView; 17 | import com.dh.notice.HelperNotificationListenerService; 18 | import java.io.IOException; 19 | import okhttp3.Call; 20 | import okhttp3.Callback; 21 | import okhttp3.FormBody; 22 | import okhttp3.Headers; 23 | import okhttp3.MediaType; 24 | import okhttp3.OkHttpClient; 25 | import okhttp3.Request; 26 | import okhttp3.RequestBody; 27 | import okhttp3.Response; 28 | 29 | 30 | public class MainActivity extends AppCompatActivity { 31 | 32 | private TextView tvMsg; 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_main); 38 | 39 | //开始监听 40 | findViewById(R.id.btStart).setOnClickListener(new View.OnClickListener() { 41 | @Override 42 | public void onClick(View v) { 43 | if (!isEnable(MainActivity.this)) { 44 | openSetting(MainActivity.this); 45 | toggleNotificationListenerService(MainActivity.this); 46 | } 47 | } 48 | }); 49 | tvMsg = findViewById(R.id.tvMsg); 50 | 51 | registBroadCast(); 52 | } 53 | 54 | private void openSetting(Context context) { 55 | Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); 56 | if (!(context instanceof Activity)) { 57 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 58 | } 59 | context.startActivity(intent); 60 | } 61 | 62 | private static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners"; 63 | 64 | private boolean isEnable(Context context) { 65 | String pkgName = context.getPackageName(); 66 | final String flat = Settings.Secure.getString(context.getContentResolver(), 67 | ENABLED_NOTIFICATION_LISTENERS); 68 | if (!TextUtils.isEmpty(flat)) { 69 | final String[] names = flat.split(":"); 70 | for (int i = 0; i < names.length; i++) { 71 | final ComponentName cn = ComponentName.unflattenFromString(names[i]); 72 | if (cn != null) { 73 | if (TextUtils.equals(pkgName, cn.getPackageName())) { 74 | return true; 75 | } 76 | } 77 | } 78 | } 79 | return false; 80 | } 81 | 82 | 83 | private void toggleNotificationListenerService(Context context) { 84 | PackageManager pm = context.getPackageManager(); 85 | pm.setComponentEnabledSetting( 86 | new ComponentName(context, HelperNotificationListenerService.class), 87 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 88 | 89 | pm.setComponentEnabledSetting( 90 | new ComponentName(context, HelperNotificationListenerService.class), 91 | PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 92 | } 93 | 94 | private void registBroadCast() { 95 | IntentFilter filter = new IntentFilter(HelperNotificationListenerService.SEND_MSG_BROADCAST); 96 | registerReceiver(receiver, filter); 97 | } 98 | 99 | private BroadcastReceiver receiver = new BroadcastReceiver() { 100 | @Override 101 | public void onReceive(Context context, Intent intent) { 102 | String msg = intent.getStringExtra("msg"); 103 | send(msg); 104 | tvMsg.setText(msg); 105 | } 106 | }; 107 | 108 | private void send(String context) { 109 | System.out.println("发送:"+context); 110 | OkHttpClient okHttpClient = new OkHttpClient(); 111 | RequestBody requestBody = new FormBody.Builder() 112 | .add("context", context) 113 | .build(); 114 | Request request = new Request.Builder() 115 | .url("https://denghao.me/special/appNotice/get.php") 116 | .post(requestBody) 117 | .build(); 118 | 119 | okHttpClient.newCall(request).enqueue(new Callback() { 120 | @Override 121 | public void onFailure(Call call, IOException e) { 122 | System.out.println("发送失败 onFailure: " + e.getMessage()); 123 | } 124 | 125 | @Override 126 | public void onResponse(Call call, Response response) throws IOException { 127 | System.out.println("响应内容 body: " + response.body().string()); 128 | } 129 | }); 130 | 131 | } 132 | 133 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 |