MarcForn
MarcForn

Reputation: 3399

Notification closed when Service finished

I'm creating a notification and updating it during the execution of an intent service. The problem appears when the services finished, also the notification is canceled.

intentNotification = new Intent(this, ScanProcessActivity.class);
    intentNotification.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mBuilder = new NotificationCompat.Builder(getApplicationContext());

    mBuilder.setSmallIcon(R.mipmap.ic_app)
            .setAutoCancel(true)
            .setOngoing(true)
            .setContentTitle("Title")
            .setContentText("text")
            .setWhen(System.currentTimeMillis());

What can I do to preserve the notification after the Intent Service is finished?

Thanks in advance.

Upvotes: 1

Views: 531

Answers (1)

MarcForn
MarcForn

Reputation: 3399

Finally I've found a solution.

The main problem was that I was using:

startForeground(NOTIFICATION_ID, mBuilder.build());

which cause unexpected behaviour such as cancel notification when thread is finished.

The solution was to change the startForeground() call to:

mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

Upvotes: 0

Related Questions