【问题标题】:Android: How to distinguish CLEAR all events from notification bar from user actionAndroid:如何区分从通知栏清除所有事件和用户操作
【发布时间】:2017-01-05 11:40:10
【问题描述】:

根据specification.setDeleteIntent(pendingIntent) 与两个动作相关联(从通知栏清除所有事件和滑动等用户动作)。

我的要求是当用户触摸通知栏上出现的通知时,必须转发到NotificationsList.class。这是通过我的 pendingInent 完成的:

PendingIntent sendPendingIntent = PendingIntent.getActivity(context, reminderId, new Intent(context, NotificationsList.class), PendingIntent.FLAG_UPDATE_CURRENT);

但是,在单击 CLEAR 按钮时,用户不得被导航到应用程序。使用.setDeleteIntent(pendingIndent) 我无法满足第二个要求。用户仍被导航到 NotificationsList.class。

有没有办法以编程方式将 CLEAR 按钮触发的所有通知事件 CLEAR 与用户操作(如触摸或滑动通知栏上的特定通知)区分开来?

【问题讨论】:

  • 您找到问题的答案了吗?您需要更多帮助吗?如果你这样做,请告诉我!
  • 兄弟你还需要帮助吗?为什么你不接受任何答案?
  • 如果您找到问题的答案,请接受下面的一个,或修改您的问题,以便我可以为您提供更多帮助!

标签: android notifications android-notification-bar


【解决方案1】:

您所描述的是非常迟钝的行为。您只需将待处理的 Intent 设置为您的通知,当它被点击时,支持它的 Intent 将被执行。

如果您的代码在清除通知后将用户导航回应用程序,那么您的设计已经存在问题。如果用户清除了您的通知,您不应该尝试将它们导航回来。因此,setDeleteIntent() 不应与启动任何活动相关联。

请注意,当您单击通知(setContentIntent()) 并清除(setDeleteIntent()) 通知时支持的意图基本上是两个PendingIntent,它们不应相同,这就是您所描述的问题。

【讨论】:

    【解决方案2】:

    您无法区分这两个事件。正如documentation 所说:

    在发生以下任一情况之前,通知一直可见:

    • 用户可以单独或使用“全部清除”(如果可以清除通知)来关闭通知。
    • 用户单击通知,您在创建通知时调用了 setAutoCancel()。
    • 您致电 cancel() 获取特定通知 ID。此方法还会删除正在进行的通知。
    • 您拨打cancelAll(),这会删除您之前发出的所有通知。

    所以在程序员看来基本上有三种不同的事件:

    • 您关闭了通知
    • 用户点击通知
    • 用户关闭通知(通过滑动或清除通知)

    第一个事件由您自己通过调用cancelAll()cancel() 触发。

    你可以处理第二个喜欢(我想你想做的):

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            //....
            .setContentIntent(sendPendingIntent);
    

    您可以像处理第三个事件一样(如上所述):

    builder.setDeleteIntent(pendingIndent)
    

    我不建议在用户关闭您的通知后开始活动,因为用户不会期待它,这将是一个糟糕的用户体验。

    我希望我能帮上忙。

    【讨论】:

      【解决方案3】:

      根据design guidelines,用户可以期望使用单击、滑动和捏缩放等高级手势与您的通知进行交互。立即响应触摸等较低级别的事件会使这些手势短路,因此您的要求将违反设计准则,您不应实施它。

      如果改变需求,让用户点击通知时被转发,则无需区分刷卡和清除,这无论如何都是不可能的。

      所以你的问题应该通过改变需求中的一个词来解决:touch --> click。

      【讨论】:

        【解决方案4】:

        我在 Google 上搜索了 deleteIntent 以查找导致我来到这里的问题的一些信息。

        英语是我的第二语言。对不起,有些用词提前。 我是一个安卓新手,如果答案很糟糕,请投反对票:)

        对于你的最后一个问题,正如 @x-code@bendaf 所说,不可能 区分刷卡和清除。

        我正在关注codelabs about notifications,遇到了同样的问题(标题中的描述)。所以我决定提供更多关于如何在你的情况下使用.setDeleteIntent 的细节。也许你已经这样做了。

        在您的情况下,包装的意图用于启动活动,pendingIntent 也是如此。

        PendingIntent sendPendingIntent = PendingIntent.getActivity(context, reminderId, new Intent(context, NotificationsList.class), PendingIntent.FLAG_UPDATE_CURRENT);
        

        但是为了进行广播,例如清除通知后做一些事情,使用:

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFICATION_ID, new Intent(yourCustomActionString), PendingIntent.FLAG_UPDATE_CURRENT);
        
        builder.setDeleteIntent(pendingIntent); // the pendingIntent will be sent when the notification is cleared
        

        然后我们需要一个自定义广播接收器接收 Intent 对象中包含的自定义操作,在您的情况下,此操作与清除有关:

        // Inside onCreate, register the broadcast receiver;
        registerReceiver(new MyReceiver(), new IntentFilter(yourCustomActionString));
        .
        .
        // Create an inner class
        public class MyReceiver extends BroadcastReceiver {
            public NotificationReceiver() {}
            @Override
            public void onReceive(Context context, Intent intent) {
                // code inside will be executed when pendingIntent is sent
                Log("taG", "Notification is cleared"); // a message will be logged if the notification is cleared
                // for more than one action, using switch...case to decide
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-24
          • 2019-01-12
          • 1970-01-01
          相关资源
          最近更新 更多