Android通知IntentService中的deleteIntent调用函数

Android notification deleteIntent call function in IntentService

您好!

我有一个扩展 IntentService 的 class。它检查新的 DB 条目并在找到新条目时显示通知,休眠几秒钟并重复。
所有已显示(“已通知”)条目都保存在本地“忽略列表”中,并具有与数据库中相同的通知 ID。

现在我的问题是:

当通知被取消时(即用户将其滑开或点击“全部清除”),我想将 notification/the 数据库条目的 ID 添加到“忽略列表”中,这样几秒后没有新的通知。
但是我不知道如何在对取消做出反应时获取通知ID。
使用 deleteIntent 时如何获取 Notification 的属性(例如 ID、contentTitle、contentText 等)?

编辑:

显示通知的函数:

private void showNotification(String title, String text, int icon, int notificationID) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setContentTitle(title);
    mBuilder.setContentText(text);
    mBuilder.setSmallIcon(icon);

    Intent intent = new Intent(this, NotificationDeleteReceiver.class);
    intent.putExtra("id", notificationID);
    intent.putExtra("title", title);
    intent.putExtra("text", text);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setDeleteIntent(pendingIntent);

    mNotificationManager.notify(notificationID, mBuilder.build());
}

接收器中的反应:

@Override
public void onReceive(Context context, Intent intent) {
    int id = intent.getExtras().getInt("id");
    String title = intent.getExtras().getString("title");
    String text = intent.getExtras().getString("text");

    Log.v("qwerty", "Notification has been deleted!\nid: " + id + "\ntitle: " + title + "\ntext: " + text);
}
      Intent intent = new Intent(this, yourBrodcastReceiver.class);
      intent.putExtra("notificationId",notificationId)
      intent.setAction(Long.toString(System.currentTimeMillis()))
      // pass all the value in intent
     PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      Builder builder = new Notification.Builder(this):
      builder.setDeleteIntent(pendingIntent); // this is delete intent it will call when you cancel or swipe

yourBrodcastReceiver class 是广播接收器,您可以在其中执行此操作..

您可以通过

获取通知id

int notificationId = intent.getExtras().getInt("notificationId"); //get all value in intent

您可以将所有数据传递给意图服务..通过意图