【问题标题】:Custom Notification RemoteView dismiss on swipe Left or Right自定义通知 RemoteView 在向左或向右滑动时关闭
【发布时间】:2020-05-22 04:18:38
【问题描述】:

我使用自定义布局的自定义音频播放器通知。

simpleContentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.custom_notification);
expandedView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.big_notification);

并创建通知

notification = new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("KJV Bible")
            //.setOngoing(false)
            .setAutoCancel(true)
            .setDeleteIntent(createOnDismissedIntent(this))
            .setChannelId(CHANNEL_ID).build();

并添加自定义视图的监听器

public void setListeners(RemoteViews view) {
    Intent previous = new Intent(NOTIFY_PREVIOUS);
    Intent delete = new Intent(NOTIFY_DELETE);
    Intent pause = new Intent(NOTIFY_PAUSE);
    Intent next = new Intent(NOTIFY_NEXT);
    Intent play = new Intent(NOTIFY_PLAY);

    mMyBroadcastReceiver = new NotificationBroadcast(controls);
    IntentFilter filterPrevious = new IntentFilter(NOTIFY_PREVIOUS);
    this.registerReceiver(mMyBroadcastReceiver, filterPrevious);

    IntentFilter filterDelete = new IntentFilter(NOTIFY_DELETE);
    this.registerReceiver(mMyBroadcastReceiver, filterDelete);

    IntentFilter filterPause = new IntentFilter(NOTIFY_PAUSE);
    this.registerReceiver(mMyBroadcastReceiver, filterPause);

    IntentFilter filterNext = new IntentFilter(NOTIFY_NEXT);
    this.registerReceiver(mMyBroadcastReceiver, filterNext);

    IntentFilter filterPlay = new IntentFilter(NOTIFY_PLAY);
    this.registerReceiver(mMyBroadcastReceiver, filterPlay);

    PendingIntent pPrevious = PendingIntent.getBroadcast(getApplicationContext(), 0, previous, PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.btnPrevious, pPrevious);

    PendingIntent pDelete = PendingIntent.getBroadcast(getApplicationContext(), 0, delete, PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.btnDelete, pDelete);

    PendingIntent pPause = PendingIntent.getBroadcast(getApplicationContext(), 0, pause, PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.btnPause, pPause);

    PendingIntent pNext = PendingIntent.getBroadcast(getApplicationContext(), 0, next, PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.btnNext, pNext);

    PendingIntent pPlay = PendingIntent.getBroadcast(getApplicationContext(), 0, play, PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.btnPlay, pPlay);
}

现在,当播放器处于播放模式时,当播放器 id 暂停而不打开时,我如何删除或关闭此通知?

我使用了 setDeleteIntent() 但效果不佳。

private PendingIntent createOnDismissedIntent(Context context) {
    Intent intent = new Intent(context, NotificationDismissedReceiver.class);
    intent.putExtra("notificationId", NOTIFICATION_ID);
    return PendingIntent.getBroadcast(context.getApplicationContext(), NOTIFICATION_ID, intent, 0);


    return PendingIntent.getBroadcast(this, 0, new Intent(NOTIFY_DELETE), PendingIntent.FLAG_UPDATE_CURRENT);
}

【问题讨论】:

  • setOngoing(false) 应该这样做。
  • 正在尝试但无法正常工作,请对此发表评论。

标签: android notifications dismiss


【解决方案1】:

我认为您应该使用 PendingIntent.FLAG_CANCEL_CURRENT 标志来关闭 createOnDismissedIntent 方法中的通知。

下面的更新方法 -

private PendingIntent createOnDismissedIntent(Context context) {
    Intent intent = new Intent(context, NotificationDismissedReceiver.class);
    intent.putExtra("notificationId", NOTIFICATION_ID);
    return PendingIntent.getBroadcast(context.getApplicationContext(), NOTIFICATION_ID, intent, 0);


    return PendingIntent.getBroadcast(this, 0, new Intent(NOTIFY_DELETE), PendingIntent.FLAG_CANCEL_CURRENT);
}

【讨论】:

  • 你能解释一下为什么 PendingIntent.FLAG_CANCEL_CURRENT 吗?
  • 我在服务中使用自定义布局通知,这就是为什么这段代码不起作用的原因,b'coz 当服务创建通知被创建为这样的前台 startForeground(NOTIFICATION_ID, notification);
【解决方案2】:

只需使用这个:

private PendingIntent createOnDismissedIntent(Context context) {
    Intent intent = new Intent(context, NotificationDismissedReceiver.class);
    intent.putExtra("notificationId", NOTIFICATION_ID);
    return PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

【讨论】:

    猜你喜欢
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    相关资源
    最近更新 更多