【问题标题】:Notification is not being dismissed (Android)通知未被解除(Android)
【发布时间】:2016-10-02 09:52:17
【问题描述】:

点击操作时通知 setAutoCancel(true) 不起作用

我有一个通知,里面有一个动作。当我点击通知时,它会从列表中删除。但是,当我单击操作时,它成功完成了操作(即拨打电话),但是当我返回通知列表时,它仍然存在。 AlarmReceiver的相关代码:

public class AlarmReceiver extends BroadcastReceiver {
Meeting meeting;

/**
 * Handle received notifications about meetings that are going to start
 */
@Override
public void onReceive(Context context, Intent intent) {

    // Get extras from the notification intent
    Bundle extras = intent.getExtras();
    this.meeting = extras.getParcelable("MeetingParcel");

    // build notification pending intent to go to the details page when click on the body of the notification
    Intent notificationIntent = new Intent(context, MeetingDetails.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    notificationIntent.putExtra("MeetingParcel", meeting);      // send meeting that we received to the MeetingDetails class
    notificationIntent.putExtra("notificationIntent", true);    // flag to know where the details screen is opening from

    PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    // build intents for the call now button
    Intent phoneCall = Call._callIntent(meeting);
    if (phoneCall != null) {

        PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);

        int flags = Notification.FLAG_AUTO_CANCEL;

        // build notification object
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Notification notification = builder.setContentTitle("Call In")
                .setContentText(intent.getStringExtra("contextText"))
                .setTicker("Call In Notification")
                .setColor(ContextCompat.getColor(context, R.color.colorBluePrimary))
                .setAutoCancel(true)                    // will remove notification from the status bar once is clicked
                .setDefaults(Notification.DEFAULT_ALL)  // Default vibration, default sound, default LED: requires VIBRATE permission
                .setSmallIcon(R.drawable.icon_notifications)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(meeting.description))
                .addAction(R.drawable.icon_device, "Call Now", phoneCallIntent)
                .setCategory(Notification.CATEGORY_EVENT)   // handle notification as a calendar event
                .setPriority(Notification.PRIORITY_HIGH)    // this will show the notification floating. Priority is high because it is a time sensitive notification
                .setContentIntent(pIntent).build();

        notification.flags = flags;

        // tell the notification manager to notify the user with our custom notification
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notification);
    }
  }
}

【问题讨论】:

  • 异步 - 你找到解决这个问题的方法了吗。目前我也面临同样的问题?

标签: android notifications dismiss


【解决方案1】:

使用这个标志:

Notification.FLAG_AUTO_CANCEL 在这里面:

int flags = Notification.FLAG_AUTO_CANCEL;
Notification notification = builder.build();
        notification.flags = flags;

Documentation

【讨论】:

  • 我按照您的建议更新了代码,但仍然没有运气.. 请在问题中查看我编辑的代码(正确吗?)
  • @Async- ,是的。必须尝试使用​​非常非常简单的通知来测试?
  • 好的,事实证明这已经是一个已知问题,我将发布有关此问题的其他 stackoverflow 问题的链接
  • @Async- ,您是否尝试过重启设备?
【解决方案2】:

我今天遇到了这个问题,发现 FLAG_AUTO_CANCEL 和 setAutoCanel(true) 在点击通知时都可以工作, 但不适用于动作点击

很简单,在目标服务或活动中,取消通知

NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancelAll();

或者如果有更多通知

manager.cancel(notificationId);

【讨论】:

    【解决方案3】:

    您已经在两者中创建了两个待处理的意图使用并更改了标志。

      PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);
    
      PendingIntent phoneCallIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);
    

    // 改成这一行

       PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    
       PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);
    

    【讨论】:

    • 抱歉,没有帮助
    • 仍然没有取消通知:(
    【解决方案4】:

    好吧,事实证明这已经是一个已知问题,需要完成额外的代码(通过 id 保持对通知的引用)。不知道为什么 API 不提供此功能,因为这样做似乎很合乎逻辑。但无论如何,

    看到这个answer in stackoverflow:

    当您在通知管理器上调用 notify 时,您给了它一个 id - 这是您以后可以用来访问它的唯一 id(来自通知管理器:

    notify(int id, Notification notification)
    

    要取消,您可以调用:

    cancel(int id)
    

    具有相同的 ID。所以,基本上,您需要跟踪 id 或者可能将 id 放入您添加到 PendingIntent 内的 Intent 的 Bundle 中?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 1970-01-01
      相关资源
      最近更新 更多