Removing notification after click

后端 未结 6 1444
臣服心动
臣服心动 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: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.

提交回复
热议问题