点击后删除通知

编程入门 行业动态 更新时间:2024-10-15 06:15:23
本文介绍了点击后删除通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚刚开始处理通知,现在我试图删除通知,并在通知中心点击通知后启动应用程序.

I just started working with notifications and now I'm trying to remove the notification and launch the app once the notification has been tapped in the notificationcenter.

我尝试使用以下代码:

import android.app.NotificationManager; public class ExpandNotification { private int NOTIFICATION = 546; private NotificationManager mNM; public void onCreate() { mNM.cancel(NOTIFICATION); setContentView(R.layout.activity_on); //Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show(); }

我认为此代码在被点击时会执行另一个类吗?

I think this code executes the other class when tapped?

PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0);

但是,通知不会消失,应用程序也不会启动.但是我可以向左或向右滑动将其删除,但这不是我想要的.

However the notification doesn't go away, nor does the application launch. But I'm able to swipe it to left or right to remove it but that's not what I want..

推荐答案

使用标志 Notification.FLAG_AUTO_CANCEL

Notification notification = new Notification(icon, tickerText, when); notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent); // Cancel the notification after its selected notification.flags |= Notification.FLAG_AUTO_CANCEL;

并启动应用程序:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); // Create a new intent which will be fired if you click on the notification Intent intent = new Intent(context, App.class); // Attach the intent to a pending intent PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

更多推荐

点击后删除通知

本文发布于:2023-11-27 19:49:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!