'How to remove sticky notification?

I am new to android. I have already developed android application which starts service when application starts.

What I have : Currently when application starts, it creates sticky notification in notification area as shown in screenshot.

enter image description here

Source code for service :

public class InfiniteService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    Intent notificationIntent = new Intent();

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);



    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name)) 
            .setContentText("Welcome To TestApp") 
            .setContentIntent(pendingIntent).build();

    startForeground(1337, notification);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //Log.e("InfiniteService", "Service started");
    return START_STICKY;
}


@Override
public void onDestroy() {
    super.onDestroy();
    //Log.e("InfiniteService", "onDestroy");
    sendBroadcast(new Intent("InfiniteService"));
}

What I want : I want to get rid of this sticky notification in notification area. Instead I want service that runs in background continuously.

What I have tried :

  • I tried 'return START_NON_STICKY' in onStartCommand with initial impression that it will remove sticky notification but later learned from here that START_STICKY tells the OS to recreate the service after it has enough memory

  • I was suspecting PendingIntent is making this but again after reading explanation, come to know that it is not what I am looking for.

  • Then I searched how to create sticky notification in android hoping to get some clue. After reading this article, got to know flags makes it sticky. I searched "Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT" in my entire code but did not find anything.

What am I missing? Any pointers/help would be great. Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source