Remove notification after clicking

前端 未结 5 1031
刺人心
刺人心 2020-12-13 07:47

I want that the notification will be closed after the user is clicking on it. I saw that everybody saying to use flags, but I can\'t find the flags anywhere because I\'m usi

相关标签:
5条回答
  • 2020-12-13 08:18

    In kotlin, you can use:

    mBuilder.build().flags.and(Notification.FLAG_AUTO_CANCEL)
    
    0 讨论(0)
  • 2020-12-13 08:20

    Easy, simply call this:

    mBuilder.setAutoCancel(true);
    

    Also, while it's not really necessary, if you really want to use FLAG_AUTO_CANCEL, just call this before you call mNotificationManager.notify:

    mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
    
    0 讨论(0)
  • 2020-12-13 08:34

    Try this....

    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
     ..........
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.push_notify_icon)
                .setContentTitle("New Question!")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setAutoCancel(true).setContentText("" + QuestionData.getAuthor().getUserName() + ": " + QuestionData.getQuestion() + "");
    mBuilder.setContentIntent(contentIntent);
    
        ..............        
    
    
    mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(0, mBuilder.build());
    
    0 讨论(0)
  • 2020-12-13 08:43

    Here is notification:

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_calendar)
                .setContentTitle("My Firebase Push notification")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(soundUri)
                .setContentIntent(pendingIntent);
    

    the key behind cancellation on click is:

                .setAutoCancel(true)
    

    i hope it resolves the matter.

    0 讨论(0)
  • 2020-12-13 08:44

    You can add a flag to your notification:

    http://developer.android.com/reference/android/app/Notification.html#FLAG_AUTO_CANCEL

    This will dismiss it upon clicking.

    0 讨论(0)
提交回复
热议问题