我希望这并不违反任何规则,因为我已尝试按照“如何询问”指南进行操作。
我尝试使用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) {
}
}
答案 0 :(得分:2)
如果您使用的是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*/
}
}
}
答案 1 :(得分:1)
我不知道我是否想念你,但是我已经将一个代码堆叠起来并逐一显示在logcat上,只有我遇到的问题就是当我收到这个问题时第一条消息,我在logcat上的文本显示为null,在第一条消息之后,所有传入的消息都在工作。
`public class NotificationService扩展NotificationListenerService { 上下文背景; @override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
String ticker = "";
if (sbn.getNotification().tickerText != null) {
ticker = sbn.getNotification().tickerText.toString();
}
Bitmap bmp;
Bundle extras;
byte[] byteArrayS;
String encoded = null;
extras = sbn.getNotification().extras;
Log.d("extras", extras.toString());
String contato="";
String texto = "";
String search = "mensagens";
if((extras.getString("android.title").toLowerCase().contains(search.toLowerCase()))){
if(extras.getString("android.title").toLowerCase().contains("Whatsapp".toLowerCase())){
extras.getString("android.title").replace("Whatsapp ","");
Log.d("REPLACE","REPLACE CONCLUÍDO");
}
if((extras.getString("android.text").toLowerCase().contains(search.toLowerCase()))){
Log.d("MSG1","MENSAGEM NÃO AUTORIZADA");
}
}
//TRATA AS NOTIFICAÇÕES FAZENDO COM QUE CADA MENSAGEM ENTRE DE UMA EM UMA DENTRO DA LISTA.
if (extras.getCharSequence("android.text") != "") {
if(extras.getString("android.summaryText")!= null) {
contato = extras.getString("android.title");
texto = extras.getCharSequence("android.text").toString();
Log.d("TEXTO1", texto);
}
}
if(extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES) != null){
if (extras.get("android.textLines") != null) {
CharSequence[] charText = (CharSequence[]) extras
.get("android.textLines");
Log.d("CHARTEXT",charText.toString());
if (charText.length > 0) {
texto = charText[charText.length - 1].toString();
Log.d("TEXTO2",texto);
}
}
}
Log.i("ContatoINTENT",contato);
if (texto != "") {
Log.i("TextoINTENT",texto);
}
`