如何删除通知在android系统? [英] How to remove Notification in android?

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

问题描述

我正在开发中,我收到多个通知的应用程序。我成功地接收不同数据的多个通知,但是当我使用 setAutoCancel(真); 的删除通知,我收到多个通知,它显示了通知栏上只剩下最后通知。搜索结果
在这里,我的问题可见多个通知,但如果我删除 setAutoCancel(真); 方法,我可以成功地接收多个通知,但我要删除多个通知的通知。

GcmIntentService.java:

 公共类GcmIntentService扩展IntentService {
    上下文语境;    //System.currentTimeMillis();    私人NotificationManager mNotificationManager;
    NotificationCompat.Builder建设者;
    公共静态最后的字符串标记=GCM演示;    公共GcmIntentService(){
        超级(GcmIntentService);
        // TODO自动生成构造函数存根
    }    @覆盖
    保护无效onHandleIntent(意向意图){
        // TODO自动生成方法存根
        捆绑额外= intent.getExtras();
        弦乐味精= intent.getStringExtra(信息);
        Google云端通讯GCM = GoogleCloudMessaging.getInstance(本);
        字符串为messageType = gcm.getMessageType(意向);
        如果(!extras.isEmpty()){            如果(Google云端通讯。
                    MESSAGE_TYPE_SEND_ERROR.equals(为messageType)){
                sendNotification时(RegIdDTO.REG_ID,发送错误:+
                        extras.toString());
            }否则如果(Google云端通讯。
                    MESSAGE_TYPE_DELETED.equals(为messageType)){
                sendNotification时(RegIdDTO.REG_ID,在服务器上删除的邮件:+
                        extras.toString());
                //如果是一个普通GCM消息,做了一些工作。
            }否则如果(Google云端通讯。
                    MESSAGE_TYPE_MESSAGE.equals(为messageType)){
                //这个循环再presents服务做了一些工作。
                的for(int i = 0;我小于5;我++){
                    Log.i(TAG,工作......+(I + 1)
                            +/ 5 @+ SystemClock.elapsedRealtime());
                    尝试{
                        视频下载(500);
                    }赶上(InterruptedException的E){
                    }
                }
                Log.i(TAG,已完成的工作@+ SystemClock.elapsedRealtime());
                接收到的消息的//发布通知。
                // sendNotification时(收到:+ extras.toString());
                sendNotification时(RegIdDTO.REG_ID,味精);
                Log.i(TAG,收到:+ extras.toString());
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(意向);
    }
    私人无效sendNotification时(时长,弦乐味精){
        mNotificationManager =(NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);
        意图myintent =新意图(这一点,ReceiveActivity.class);
        myintent.putExtra(信息,味精);
        myintent.setData(Uri.parse(内容://+当));        的PendingIntent contentIntent = PendingIntent.getActivity(这一点,1,
                myintent,PendingIntent.FLAG_UPDATE_CURRENT);        NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(本)
                .setSmallIcon(R.drawable.ic_stat_gcm)
                .setContentTitle(事件追踪)
                .setContentText(活动获得了);
        通知通知= mBuilder.build();
        AudioManager上午=(AudioManager)this.getSystemService(Context.AUDIO_SERVICE);        / *即使模式被设置为声音和放大器;振动的电话,
         *状态code,它getRingerMode()返回的是RINGER_MODE_NORMAL。
         * /
        开关(am.getRingerMode())
        {
            案例AudioManager.RINGER_MODE_VIBRATE:
                mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
                打破;
            案例AudioManager.RINGER_MODE_NORMAL:
                mBuilder.setDefaults(Notification.DEFAULT_SOUND);
                打破;
            默认:
                mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        }        mBuilder.setContentIntent(co​​ntentIntent);
        mBuilder.setAutoCancel(真);
        mNotificationManager.notify((INT)时,mBuilder.build());    }    我接收活动    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_receive);
        意向意图= getIntent();
        名=(的TextView)findViewById(R.id.name);
        对付=(的TextView)findViewById(R.id.deal);
        有效=(的TextView)findViewById(R.id.valid);
        地址=(的TextView)findViewById(R.id.address);
        字符串消息= intent.getExtras()的getString(信息)。
        尝试{
            JSON =新的JSONObject(消息);
            字符串STIME = json.getString(名称);
            name.setText(STIME);            字符串slecturename = json.getString(交易);
            deal.setText(slecturename);            字符串sroom = json.getString(有效);
            valid.setText(sroom);            字符串sfaculty = json.getString(地址);
            address.setText(sfaculty);
        }赶上(JSONException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }    }
}


解决方案

使用的NotificationManager取消通知。你只需要提供您的通知标识

  mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

I am developing an application in which I receive multiple notifications. I am successfully receiving multiple notifications with different data, but when I use setAutoCancel(true); for remove notification, and I receive multiple notifications it shows only last notification on notification bar.

Here my problem shows multiple notification, but if I remove setAutoCancel(true); method I can successfully receive multiple notifications, but I want to remove notification with multiple notifications.

GcmIntentService.java:

public class GcmIntentService extends IntentService {
    Context context;

    //System.currentTimeMillis();

    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    public static final String TAG = "GCM Demo";

    public GcmIntentService() {
        super("GcmIntentService");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        Bundle extras = intent.getExtras();
        String msg = intent.getStringExtra("message");
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);


        if (!extras.isEmpty()) {

            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification(RegIdDTO.REG_ID,"Send error: " +
                        extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification(RegIdDTO.REG_ID,"Deleted messages on server: " +
                        extras.toString());
                // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i=0; i<5; i++) {
                    Log.i(TAG, "Working... " + (i+1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                //sendNotification("Received: " + extras.toString());
                sendNotification(RegIdDTO.REG_ID,msg);
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
    private void sendNotification(long when,String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);


        Intent myintent = new Intent(this, ReceiveActivity.class);
        myintent.putExtra("message", msg);
        myintent.setData(Uri.parse("content://"+when));

        PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
                myintent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_gcm)
                .setContentTitle("Event tracker")
                .setContentText("Events received");
        Notification notification=mBuilder.build();


        AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);

        /* Even if the mode is set to "Sound & Vibration" in the phone, 
         * the status code that getRingerMode() returns is RINGER_MODE_NORMAL.
         */
        switch (am.getRingerMode())
        {
            case AudioManager.RINGER_MODE_VIBRATE:
                mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
                break;
            case AudioManager.RINGER_MODE_NORMAL:
                mBuilder.setDefaults(Notification.DEFAULT_SOUND);
                break;
            default:
                mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        }

        mBuilder.setContentIntent(contentIntent);
        mBuilder.setAutoCancel(true);
        mNotificationManager.notify((int) when, mBuilder.build());

    }

    my receive activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);
        Intent intent = getIntent();


        name = (TextView) findViewById(R.id.name);
        deal = (TextView) findViewById(R.id.deal);
        valid = (TextView) findViewById(R.id.valid);
        address = (TextView)findViewById(R.id.address);
        String message = intent.getExtras().getString("message");
        try {
            json = new JSONObject(message);
            String stime = json.getString("name");
            name.setText(stime);

            String slecturename = json.getString("deal");
            deal.setText(slecturename);

            String sroom = json.getString("valid");
            valid.setText(sroom);

            String sfaculty = json.getString("address");
            address.setText(sfaculty);


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

解决方案

Use the NotificationManager to cancel your notification. You only need to provide your notification id

mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

这篇关于如何删除通知在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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