我正在尝试使用NotificationListenerService
解析我在设备上收到的Whatsapp通知。
第一次收到通知后,我可以检索短信。但是,当第二条消息到达时,我收到消息:“2条新消息”。所以基本上这里通知中的消息都堆叠了。
以下是我使用的代码:
public class NLService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG,"********** onNotificationPosted");
String key = sbn.getKey();
sbn.getNotification().tickerText + "\n Package Name:" + sbn.getPackageName());
Log.i(TAG,"getTAG:"+sbn.getTag());
Log.i(TAG,"getKey:"+sbn.getKey());
Log.i(TAG,"getGrpKey:"+sbn.getGroupKey());*/
Bundle bundle = sbn.getNotification().extras;
String title = bundle.getString("android.title");
String text = bundle.getCharSequence("android.text").toString();//This gives me the actual message, the first time the notification arrives
Log.i(TAG,"Bundle is:"+bundle);
Log.i(TAG,"Title:"+title);
Log.i(TAG,"Text:"+text);
NLService.this.cancelNotification(key);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i(TAG,"********** onNOtificationRemoved");
Log.i(TAG,"ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText +"\t" + sbn.getPackageName());
}
}
所以我的查询是,如何解析/获取此通知中的消息?或者是否有其他方法来获取传入的whatsapp消息?
我能想到的一种方法是将消息标记为“已读”,但有人能够以编程方式将whatsapp消息标记为“已读”吗?
我已经阅读了Stackoverflow上的大多数旧帖子,而且whatsapp官方网站为我们提供了一种发送消息的方式。
任何建议都应该非常有用。