Removing notification after click

后端 未结 6 1429
臣服心动
臣服心动 2020-12-15 15:44

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.<

相关标签:
6条回答
  • 2020-12-15 16:18

    Use the flag 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;
    

    and to launch the app:

    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);
    
    0 讨论(0)
  • 2020-12-15 16:25

    This answer is too much late but specially i write following solution because notification constructor become deprecated so use notification using builder , like following :

     **.setAutoCancel(true)** is used to remove notification on click
    

    and entire notification is like follwoing :

      private void makeNotification(String title,String msg){
    
        Intent resultIntent = new Intent(this, MasterActivity.class);
    
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
    
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setContentIntent(resultPendingIntent)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(title)
                        .setAutoCancel(true)
                        .setContentText(msg);
    
        int mNotificationId = 001;
        NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(mNotificationId, mBuilder.build());
    
    }
    

    Calling this method with title and message you get perfect notification.

    0 讨论(0)
  • 2020-12-15 16:25

    For my it was that

    .setPriority(Notification.PRIORITY_HIGH);
    

    that was causing the notification to not clear after click... make sure you use:

    .setPriority(Notification.PRIORITY_DEFAULT);
    

    And .setAutoCancel(true) should work.

    0 讨论(0)
  • 2020-12-15 16:31

    To get the same effect using Notification.Builder or NotificationCompat.Builder call setAutoCancel(true) on the Builder instance.

    0 讨论(0)
  • 2020-12-15 16:31

    Best & simple way is set builder.setAutoCancel(true) it's cancel your notification after clicking on notification. I hope this code help you.

    Builder for notification

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(android.R.drawable.btn_star);
    builder.setContentTitle("This is title of notification");
    builder.setContentText("This is a notification Text");
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    

    For open an activity on clicking notification

    Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);
    builder.setAutoCancel(true);
    

    Show bulder in notification

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(114, builder.build());
    

    Complete code for show a notification with icon, image, title, description, auto cancel and on click open an activity

    public void ShowIntentNotification(View v)
        {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setSmallIcon(android.R.drawable.btn_star);
            builder.setContentTitle("This is title of notification");
            builder.setContentText("This is a notification Text");
            builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    
            Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            builder.setContentIntent(pendingIntent);
            builder.setAutoCancel(true);
    
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.notify(114, builder.build());
    
        }
    
    0 讨论(0)
  • 2020-12-15 16:32

    You can directly add .setAutoCancel(true) this line into your code in order to remove notification on click.

    This must be added in your builder variable.

    Example:

    mBuilder = new NotificationCompat.Builder(mContext);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentTitle("Notification")
            .setContentText("Hello")
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);
    
    0 讨论(0)
提交回复
热议问题