问题是如何在堆叠时(如在Whatsapp中)获取所有传入通知的TEXT(不是标题)字段。如何在Android中获取堆叠通知的文本
public class NLService extends NotificationListenerService {
public void onNotificationPosted(StatusBarNotification sbn) {
Log.v(Constants.TAG_notifs,
"------------------------- in onNotificationPosted(), Notification Text = "
+ sbn.getNotification().tickerText);
Bundle extras = sbn.getNotification().extras;
if (extras.containsKey("android.text")) {
if (extras.getCharSequence("android.text") != null) {
String text = extras.getCharSequence("android.text").toString();
Log.v(Constants.TAG_notifs,
"------------------------- in onNotificationPosted(), Bundle.text != NULL, so here it is = "
+ text);
}
}
if (extras.containsKey("android.title")) {
Log.v(Constants.TAG_notifs,
"------------------------- in onNotificationPosted(), Bundle android.title = "
+ extras.getString("android.title"));
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
//super.onNotificationRemoved(sbn);
}
} 当WHATSAPP通知从单个用户到达此行中的第一次(字符串文本= extras.getCharSequence( “android.text”)的toString();)已成功读取文本,但在此之后,当有更多消息进入并且通知堆积(如上图所示)时,变量文本始终为NULL。
这一定是可能的,因为this app正在做它,测试它。它正在获取每个应用程序的文本。
添加激励:如果您知道答案或要尝试的事情,还有另一个问题看起来类似问题here。
当第二个通知(比如来自A的例子)进入'sbn.getNotification()。extras。getCharSequence(Notification.EXTRA_TEXT)的ToString();”是NULL,那里什么也没有。你是不是也看到它了?我正在尝试Kitkat,API = 19 – user1406716 2015-03-31 09:00:54
我不确定这是否属实,您是否可以详细说明如何获得以前的通知?:“*这种方式当新的发送者消息发出时,previoud通知也会出现,并且我们会得到回调在NotificationListener *“ – user1406716 2015-03-31 09:01:34
对于KitKat,它应该与AccessibilityService正常工作..尝试使用此服务回调..在我的情况下,AccessibilityService正常工作,直到(<= KitKat),所以请检查使用它 – Kushal 2015-03-31 09:07:46