@@ -12,6 +12,7 @@ import com.nbee.echolink.model.WeChatMsg;
import com.nbee.echolink.utils.HandleNoticeUtils ;
import com.nbee.echolink.utils.NetworkUtils ;
import java.text.DateFormat ;
import java.text.SimpleDateFormat ;
import java.util.Arrays ;
import java.util.Date ;
@@ -27,6 +28,7 @@ public class NotificationListener extends NotificationListenerService {
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 static final DateFormat DATE_FORMAT = new SimpleDateFormat ( " yyyy-MM-dd HH:mm:ss " , Locale . getDefault ( ) ) ;
private NetworkUtils networkUtil ;
private final HashMap < String , Long > recentLogs = new HashMap < > ( ) ;
private final Handler logCleanerHandler = new Handler ( Looper . getMainLooper ( ) ) ;
@@ -46,40 +48,52 @@ public class NotificationListener extends NotificationListenerService {
}
Bundle extras = sbn . getNotification ( ) . extras ;
CharSequence titleCharSequence = extras . getCharSequence ( Notification . EXTRA_TITLE ) ;
String title = titleCharSequence ! = null ? titleCharSequence . toString ( ) : null ;
String content = extras . getString ( Notification . EXTRA_TEXT ) ;
String title = getNotificationText ( extras , Notification . EXTRA_TITLE ) ;
String content = getNotificationText ( extras , Notification . EXTRA_TEXT ) ;
String tickerText = sbn . getNotification ( ) . tickerText ! = null ? sbn . getNotification ( ) . tickerText . toString ( ) : " " ;
String logMessage = String . format ( " packageName: %s,title: %s,content: %s,tickerText: %s " , packageName , title , content , tickerText ) ;
String appName = handleNoticeUtils . messageHandle ( packageName ) ;
String logMessage = String . format ( " packageName: %s, title: %s, content: %s, tickerText: %s " , packageName , title , content , tickerText ) ;
if ( " 微信 " . equals ( appName ) & & tickerText . contains ( " : " ) ) {
if ( ! shouldPrintLog ( logMessage ) ) {
return ; // 如果在30秒内已打印过, 则跳过
}
String appName = handleNoticeUtils . messageHandle ( packageName ) ;
if ( " 微信 " . equals ( appName ) ) {
handleWeChatNotification ( packageName , title , tickerText ) ;
}
Timber . d ( logMessage ) ;
}
private String getNotificationText ( Bundle extras , String key ) {
CharSequence charSequence = extras . getCharSequence ( key ) ;
return charSequence ! = null ? charSequence . toString ( ) : " " ;
}
private void handleWeChatNotification ( String packageName , String title , String tickerText ) {
if ( tickerText . contains ( " : " ) ) {
String [ ] parts = tickerText . split ( " : " , 2 ) ;
String sender = parts [ 0 ] . trim ( ) ; // 去除两端的空格
String message = parts . length > 1 ? parts [ 1 ] . trim ( ) : " " ; // 同样去除两端的空格
if ( parts . length < 2 ) return ; // 安全检查,确保不会因数组越界而崩溃
String sender = parts [ 0 ] . trim ( ) ;
String message = parts [ 1 ] . trim ( ) ;
WeChatMsg weChatMsg = new WeChatMsg ( ) ;
weChatMsg . setPackageName ( packageName ) ;
weChatMsg . setSender ( sender ) ;
weChatMsg . setMessage ( message ) ;
weChatMsg . setTitle ( title ) ;
weChatMsg . setAppName ( appName ) ;
weChatMsg . setCurrentTime ( new SimpleDateFormat ( " yyyy-MM-dd HH:mm:ss " , Locale . getDefault ( ) ) . format ( new Date ( ) ) ) ;
weChatMsg . setAppName ( " 微信 " ) ;
weChatMsg . setCurrentTime ( DATE_FORMAT . format ( new Date ( ) ) ) ;
Timber . d ( " 准备发送微信消息: %s " , weChatMsg ) ;
// 异步执行网络请求
networkUtil . postRequest ( weChatMsg ) ;
return ;
try {
Timber . d ( " 准备将接受的微信通知转发: %s " , weChatMsg ) ;
networkUtil . postRequest ( weChatMsg ) ;
} catch ( Exception e ) {
Timber . e ( e , " 转发送微信通知失败 " ) ;
}
}
// 检查是否在30秒内已打印过相同内容
if ( ! shouldPrintLog ( logMessage ) ) {
return ; // 如果在30秒内已打印过, 则跳过
}
// Timber.d(logMessage);
}
@Override
public void onNotificationRemoved ( StatusBarNotification sbn ) {
// 当通知被移除时调用