Vineesh TP
Vineesh TP

Reputation: 7973

Prevent to remove notification - Android

My application is running as a service so, I need to notify the user as application is running. I am trying to add a notification on the notification bar. Since it is a service application I need to always show the notification. How can I prevent the user to remove the notification even when the application is quit. I able to prevent the user to remove the application on swipe by using,

mBuilder.setOngoing(true);

How to show the notification even the application quit.

I have used the below code ,

private void addNotificaiononNotificationBar(){

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("My notification")
                        .setContentText("Hello World!");

        Intent resultIntent = new Intent(this, MainActivity.class);
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        mBuilder.setContentIntent(resultPendingIntent);
        mBuilder.setOngoing(true);
        // Sets an ID for the notification
        int mNotificationId = 001;
       // Gets an instance of the NotificationManager service
        NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
       // Builds the notification and issues it.
        mNotifyMgr.notify(mNotificationId, mBuilder.build());

    }

Upvotes: 1

Views: 906

Answers (1)

Andrey Danilov
Andrey Danilov

Reputation: 6602

If you want to restrict removing notification you can create Foreground Service so notification will exist when service is running.

Upvotes: 2

Related Questions