aclowkay
aclowkay

Reputation: 3877

Persistent android notification from service

How can I make a persistent notification, that will update every time the users sees it? form the service

Upvotes: 1

Views: 2317

Answers (1)

Richard Le Mesurier
Richard Le Mesurier

Reputation: 29713

To show the notification when the Service is running, you call:

    startForeground(R.string.notification_id, myNotification);

giving the method an ID for your service, and a Notification that you have created.


At any point, your Service can update what the user sees by using the same R.string.notification_id and posting a new Notification:

    NotificationManager notificationManager = 
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(R.string.notification_id, myNotification);

For creating a Notification, you need to read up on Notification.Builder (android docs here).

There is also a good answer on a related question: How exactly to use Notification.Builder? Apologies for not reposting his answer, but it includes a lot of code and will sort you out.

Upvotes: 6

Related Questions