点击后删除通知 [英] Removing notification after click

查看:57
本文介绍了点击后删除通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

这篇关于点击后删除通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆