How, when you click on the notification, delete the notification and not open the application?

4

I need to send a notification but would like it when the user clicks the notification it is deleted and does not open the application

The part of generating the notification is already working. I could not stop the application from being opened.

Thank you.

    
asked by anonymous 12.02.2016 / 21:15

2 answers

3

In order for the notification to not launch any Activity (Intent) when it is clicked, create the PendigIntent , passed to the% method of NotificationCompat.Builder , as follows:

PendingIntent resultPendingIntent = PendingIntent.getActivity(context,  0, new Intent(), 0);

Replace setContentIntent() with the values you want to use. The important thing is to pass 0 to the third parameter.

To exclude the notification when clicked use new Intent() when building the notification.

    
12.02.2016 / 21:32
0

Create notification:

private void sendNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_stat_name);
    builder.setContentTitle("My title");
    builder.setContentText("Content Text");
    Intent removeNotifivationIntent = new Intent();
    removeNotifivationIntent.setAction(REMOVE_NOTIFICATION);
    PendingIntent removeNotifivationPendingIntent = PendingIntent.getBroadcast(this, 0, removeNotifivationIntent, 0);
    builder.setContentIntent(removeNotifivationPendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

Create a BroadcastReceiver, it can be in OnCreate:

    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (REMOVE_NOTIFICATION.equals(action)) {
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
                nMgr.cancel(NOTIFICATION_ID);
            }
        }
    };
    registerReceiver(receiver, filter);
    
12.02.2016 / 21:37