IntentService中的Android通知deleteIntent调用函数

时间:2017-07-11 13:18:04

标签: java android notifications intentservice

您好!

我有一个扩展Notification的课程。它检查新的DB-entrys并在找到新条目时显示通知,休眠几秒钟并重复。
已经显示的所有内容("已通知")被保存在本地"忽略列表中"并且具有与DB中相同的通知ID。

现在我的问题:

当取消通知时(即用户将其滑开或点击"清除所有"),我想将通知的ID / DB条目添加到"忽略列表"所以几秒钟后就没有新的通知了 但我不知道在对取消作出反应时如何获取通知ID 当使用deleteIntent时,如何获取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()); } 的属性(例如ID,contentTitle,contentText等)?

编辑:

显示通知的功能:

@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);
}

接收器中的反应:

var response = {"countries":[{"country_id":730,"country":"El Salvador"},{"country_id":756,"country":"Guatemala"},{"country_id":767,"country":"Indonesia"}]};
var countries = response.countries;
for(var i = 0; i < countries.length; i++)
{
    var country_id = countries[i].country_id;
    var country = countries[i].country;
}

1 个答案:

答案 0 :(得分:1)

      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类中是广播接收器,你可以这样做..

您可以通过

获取通知ID

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

您可以通过意图

将所有数据传递给意向服务