基于Android开发者网站上的文档,onNotificationRemoved(with reason)方法可让您了解取消通知的原因,例如用户清除了所有通知,或仅清除了您的通知等。
我已经测试了其他一些回调,即onNotificationPosted和onNotificationRemoved without reason,它们已成功调用。
以下是我创建的NotificationListenerService的实现:
package com.solo.notificationlistener;
import android.app.Notification;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
public class MyNotificationListenerService extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
if (sbn.getPackageName().equals(getBaseContext().getPackageName())) {
Notification notification = sbn.getNotification();
String notificationText = notification.extras.getString(Notification.EXTRA_TEXT);
Log.d(getClass().getSimpleName(),
String.format("Just posted notification: %s", notificationText));
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
if (sbn.getPackageName().equals(getBaseContext().getPackageName())) {
Notification notification = sbn.getNotification();
String notificationText = notification.extras.getString(Notification.EXTRA_TEXT);
Log.d(getClass().getSimpleName(),
String.format("Dismissed notification: %s", notificationText));
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap, int reason) {
super.onNotificationRemoved(sbn, rankingMap, reason);
Log.d(getClass().getSimpleName(), "Dismissing with a reason");
if (sbn.getPackageName().equals(getBaseContext().getPackageName())) {
Notification notification = sbn.getNotification();
String notificationText = notification.extras.getString(Notification.EXTRA_TEXT);
Log.d(getClass().getSimpleName(),
String.format("Dismissed notification: %s\n Reason: %s",
notificationText,
(reason == NotificationListenerService.REASON_CANCEL) ?
"Specifically cancelled my app" :
"Cancelled all perhaps"
));
}
}
}
关于为什么会发生这种行为的任何想法? 非常感谢您的协助
答案 0 :(得分:0)
OnNotification删除原因是API> = 26 难道就是因为这个原因,该方法没有触发?