【发布时间】:2015-01-20 14:27:10
【问题描述】:
我希望这不会违反任何规则,因为我已尝试遵循如何提问指南。
我正在尝试使用 NotificationListenerService 读取传入的通知,它对我有用,但只是部分有效。
它的类型的第一个通知,比方说 - whatsapp 我可以得到代码、文本和标题,但是 如果通知堆积起来,我不再可以阅读消息的文本。
如何获取堆叠通知的文本?
这是我目前实现的代码:
public class NotificationService extends NotificationListenerService {
private Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
String ticker = sbn.getNotification().tickerText.toString();
Bundle extras = sbn.getNotification().extras;
String title = "";
String text = "";
if (extras.containsKey("android.title")) {
title = extras.getString("android.title");
}
if (extras.containsKey("android.text")) {
if (extras.getCharSequence("android.text") != null) {
text = extras.getCharSequence("android.text").toString();
}
}
if (pack != null) {
Log.i("Package", pack);
}
if (ticker != null) {
Log.i("ticker", ticker);
}
if (title != null) {
Log.i("Title", title);
}
if (text != null) {
Log.i("Text", text);
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
}
【问题讨论】:
-
我相信这是正确/默认的 Android 行为。我正在寻找资料来说明我的观点,但我看到了类似的问题:您只阅读了堆栈上的第一个通知。
-
例如应用程序 airdroid 使用相同的 notificationListenerService 方法,它可以正确读取通知,所以我确定有一种方法我只是不知道
-
你找到答案了吗?
-
根据文档。这是正确的行为。只会通知 FIRST/REMOVE 通知。
developer.android.com/reference/android/service/notification/… 抱歉,我没有找到任何获取通知更新的方法
标签: android android-notifications