【问题标题】:Delete notification notification.builder on content click在内容点击时删除通知notification.builder
【发布时间】:2014-11-13 11:03:51
【问题描述】:

我有一个包含 2 个操作的通知。一个意图打开一个 Activity,另一个打开浏览器网络。

我的代码:

    Intent intent1 = new Intent(context, NotificationClickActivity.class);
    PendingIntent pIntent1 = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);

    String url = "http://www.google.com";
    Intent browserIntent = new Intent(Intent.ACTION_VIEW);
    browserIntent.setData(Uri.parse(url));
    PendingIntent pBrowserIntent = PendingIntent.getActivity(context, 1, browserIntent, PendingIntent.FLAG_CANCEL_CURRENT);



    // Build notification
    Notification noti = new Notification.Builder(context)
            .setContentTitle(title)
            .setContentText(shortDescription)
            .setStyle(new Notification.BigTextStyle().bigText(longDescription))
            .setSmallIcon(R.drawable.small_defaulticon)
            .setAutoCancel(true)
            .setContentIntent(pIntent1)
            .addAction(R.drawable.playstore_icon_32, "Dettagli", pIntent1)
            .setContentIntent(pBrowserIntent)
            .addAction(R.drawable.small_defaulticon, "Vai al sito", pBrowserIntent).build();


    NotificationManager notificationManager = 
      (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

    // Hide the notification after its selected
    noti.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, noti); 

如果我点击通知,它就会消失,但如果我点击 2 个操作之一,它会打开正确的意图,但不会清除通知。 我也尝试了.setDeleteIntent(myPendingIntent),但什么都没有……我想知道我哪里出错了……

我的通知屏幕...

谢谢!

【问题讨论】:

    标签: android notifications android-notifications android-pendingintent


    【解决方案1】:

    当按下操作按钮时,通知永远不会消失。它只是向您发送相关的意图。如果您想取消它,您需要致电NoficationManager.cancel(int id) 自行取消。您通常会使用处理操作按钮意图的相同方法来执行此操作。

    您尝试使用的Notification.FLAG_AUTO_CANCEL 标志仅应用于通知正文而不应用于操作按钮。

    【讨论】:

    • 感谢您的回答。我不明白我应该把NotificationManager.cancel(int id)放在哪里。我的第一个操作按钮启动了一项活动,我应该输入该活动的onCreate 吗?但是第二个动作启动了浏览器意图..
    • 对于第一个动作,您可以将此代码放入该活动的onCreate()。对于第二个动作,您可以按以下方式进行。你需要先开始你的活动,然后调用cancel(),启动浏览器意图,然后立即finish()你的活动。您需要在onCreate() 方法中执行所有这些操作。这将是一个没有布局的代理活动。
    • 我试过了。在第一种情况下一切正常 :) 但是第二个操作没有:我创建了一个“BrowserActivity”并在此类的onCreate 中删除了通知并使用startActivity(browserIntent) 启动浏览器意图但BrowserActivity从未被调用..我不知道为什么..
    • 你能发布你浏览器意图的代码吗?也不要忘记在浏览器启动后拨打finish()
    • 完成!该死的显化!由于我从事通知库项目,我忘记在主项目 manifest.xml 中添加BrowserActivity!感谢您的支持,正确答案和 +1。
    【解决方案2】:

    在您的操作启动的活动中使用 cancel() 来关闭通知

    【讨论】:

      【解决方案3】:

      试试这个

      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
          .setDefaults(Notification.DEFAULT_ALL);
      

      并尝试在notificationManager.notify(uniqueId, noti); 中发送uniqueId

      【讨论】:

      • 感谢您的回答,“uniqueId”是 2 个操作按钮的唯一 ID 还是通知的唯一编号? NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)替换我的Notification noti = new Notification.Builder(context)
      • uniqueId 是通知的唯一编号。是的,替换它 Notification noti = new Notification.Builder(context) is depreciated now.
      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多