【问题标题】:How to remove Notification in android?如何在android中删除通知?
【发布时间】:2014-03-27 03:48:16
【问题描述】:

我正在开发一个接收多个通知的应用程序。我成功接收到包含不同数据的多个通知,但是当我使用 setAutoCancel(true); 删除通知时,我收到多个通知,它仅在通知栏上显示最后一个通知。

这里我的问题显示了多个通知,但是如果我删除 setAutoCancel(true); 方法我可以成功接收多个通知,但我想删除多个通知的通知。

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

    }
}

【问题讨论】:

  • 你的问题是什么?删除通知或显示的多个通知?
  • 这里我的问题显示多个通知,但如果我删除 setAutoCancel(true);方法我可以成功接收多个通知,但我想删除多个通知的通知
  • 要删除您可以使用 id 处理的通知,请参阅stackoverflow.com/questions/3595232/…
  • 你也是这么写的 public void CancelNotification(Context ctx) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager nMgr = (NotificationManager) ctx .getSystemService(ns); nMgr.cancel(RegIdDTO.REG_ID); }
  • 也是同样的问题

标签: android google-cloud-messaging


【解决方案1】:

NotificationManager 取消你的通知:

NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyManager.cancel(your_Notification_id);

【讨论】:

    【解决方案2】:

    使用 NotificationManager 取消通知。您只需要提供您的通知 ID

    mNotificationManager.cancel(YOUR_NOTIFICATION_ID);
    

    【讨论】:

    • 是的,我是这样写的,但它在通知栏上只显示一个通知 public void CancelNotification(Context ctx) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager nMgr = (NotificationManager) ctx .getSystemService(ns); nMgr.cancel(RegIdDTO.REG_ID); } 公共类 RegIdDTO { 公共静态最终 int REG_ID= (int) Calendar.getInstance().getTimeInMillis(); }
    • 并在接收活动中 @Override protected void onResume() { // TODO 自动生成的方法存根 super.onResume();服务=新 GcmIntentService(); serv.CancelNotification(getApplicationContext()); }
    猜你喜欢
    • 2014-12-22
    • 2011-04-05
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多