【问题标题】:Close Notification on Selection of GCM Notification Android关闭 GCM Notification Android 选择通知
【发布时间】:2013-09-03 09:36:50
【问题描述】:

我想在用户选择通知时关闭通知。这是我用来向用户显示通知的代码。我在 GCM (Google Cloud Messaging) 中使用它

private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, DemoActivity.class), 0);
       NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle(" Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg.toString());

        mBuilder.build().flags = Notification.FLAG_AUTO_CANCEL;
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(notificationSound);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

但是当我选择它正在打开活动但没有关闭通知的通知时,这对我来说不起作用。请告诉我这里出了什么问题

【问题讨论】:

    标签: android android-intent google-cloud-messaging android-notifications


    【解决方案1】:

    试试

    mNotificationManager = (NotificationManager)
                    this.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel(NOTIFICATION_ID); // use this to cancel notification
    

    或者你可以试试

    mBuilder.setAutoCancel(true); //所以当用户在面板中点击通知时会自动取消通知

    【讨论】:

      【解决方案2】:

      只需删除这些行:

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

      mBuilder.setContentIntent(contentIntent);
      

      【讨论】:

      • @Gaurav 你有没有通过这个实现你想要的,如果是,那么让它被接受
      【解决方案3】:

      使用setAutoCancel (boolean autoCancel)

      Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. The PendingIntent set with setDeleteIntent(PendingIntent) will be broadcast when the notification is canceled.

      参考: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setAutoCancel(boolean)

      【讨论】:

        【解决方案4】:
                    Notification notification = new Notification(
                            YOUR_DRAWABLE_RESOURCE, "Message",
                            System.currentTimeMillis());
                    notification.flags = Notification.FLAG_AUTO_CANCEL;
        

        您可以使用FLAG_AUTO_CANCEL 取消选中时的通知。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-14
          • 1970-01-01
          • 1970-01-01
          • 2015-05-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多