我希望这不会违反任何规则,因为我已经尝试按照“如何询问”指南进行操作。
我正在尝试使用 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 7.0+,WhatsApp 使用 MessageStyle Expanded Notifications。这里 - https://developer.android.com/training/notify-user/expanded.html#message-style
从通知中检索所有 5 条消息,例如
MyFriend (5 messages)
testt
做这个:
Bundle extras = mysbn.getNotification().extras;
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)){
Parcelable b[] = (Parcelable[]) extras.get(Notification.EXTRA_MESSAGES);
if(b != null){
content = "";
for (Parcelable tmp : b){
Bundle msgBundle = (Bundle) tmp;
content = content + msgBundle.getString("text") + "\n";
/*Set<String> io = msgBundle.keySet(); // To get the keys available for this bundle*/
}
}
}
关于android - NotificationListenerService 不读取堆叠通知的文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28047767/