创建 NotificationListener.java
parent
d52b837175
commit
1ecf3998ad
|
|
@ -0,0 +1,74 @@
|
||||||
|
package com.nbee.echolink.service;
|
||||||
|
|
||||||
|
import android.app.Notification;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.service.notification.NotificationListenerService;
|
||||||
|
import android.service.notification.StatusBarNotification;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.nbee.echolink.model.WeChatMsg;
|
||||||
|
import com.nbee.echolink.utils.HandleNoticeUtils;
|
||||||
|
import com.nbee.echolink.utils.NetworkUtil;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
|
||||||
|
public class NotificationListener extends NotificationListenerService {
|
||||||
|
private static final String TAG = "NotificationListener";
|
||||||
|
private static final List<String> notAllowList = Arrays.asList("com.github.kr328.clash","com.google.android.dialer",
|
||||||
|
"com.google.android.apps.messaging");
|
||||||
|
private HandleNoticeUtils handleNoticeUtils;
|
||||||
|
private NetworkUtil networkUtil;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
handleNoticeUtils = new HandleNoticeUtils(this);
|
||||||
|
networkUtil = new NetworkUtil(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNotificationPosted(StatusBarNotification sbn) {
|
||||||
|
String packageName = sbn.getPackageName();
|
||||||
|
if (notAllowList.contains(packageName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Bundle extras = sbn.getNotification().extras;
|
||||||
|
String title = extras.getString(Notification.EXTRA_TITLE);
|
||||||
|
String content = extras.getString(Notification.EXTRA_TEXT);
|
||||||
|
String tickerText = sbn.getNotification().tickerText != null ? sbn.getNotification().tickerText.toString() : "";
|
||||||
|
Timber.d("packageName: %s,title: %s,content: %s,tickerText: %s",packageName,title,content,tickerText);
|
||||||
|
|
||||||
|
String appName = handleNoticeUtils.messageHandle(packageName);
|
||||||
|
if ("微信".equals(appName) && tickerText.contains(":")) {
|
||||||
|
String[] parts = tickerText.split(":", 2);
|
||||||
|
WeChatMsg weChatMsg = new WeChatMsg();
|
||||||
|
weChatMsg.setPackageName(packageName);
|
||||||
|
weChatMsg.setSender(parts[0]);
|
||||||
|
weChatMsg.setMessage(parts.length > 1 ? parts[1] : "");
|
||||||
|
weChatMsg.setTitle(title);
|
||||||
|
weChatMsg.setAppName(appName);
|
||||||
|
weChatMsg.setCurrentTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()));
|
||||||
|
|
||||||
|
Timber.d("准备发送微信消息: %s", weChatMsg);
|
||||||
|
// 异步执行网络请求
|
||||||
|
networkUtil.postRequest(weChatMsg);
|
||||||
|
} else {
|
||||||
|
Timber.d("非微信通知或TickerText格式不正确,已忽略");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNotificationRemoved(StatusBarNotification sbn) {
|
||||||
|
// 当通知被移除时调用
|
||||||
|
Log.d(TAG, "通知被移除: " + sbn.getPackageName());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue