Sartheris Stormhammer
Sartheris Stormhammer

Reputation: 2564

Update foreground service notificaton from Activity

I have a foreground service in my app, which has a dynamically updated text in the notification, and I can update it just fine from withing the service with

    startForeground(Constants.FOREGROUND_ID, buildForegroundNotification());
}

private Notification buildForegroundNotification() {
    NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    b.setOngoing(true);
    b.setContentIntent(getPendingIntent(this));
    b.setContentTitle(getString(R.string.app_name));
    b.setContentText(getString(R.string.time) + " " + number);
    b.setSmallIcon(R.drawable.ic_launcher);
    return b.build();
}

But I want to also be able to update it from the Activity, how can I do that without restarting the service?

Upvotes: 0

Views: 1442

Answers (1)

Kiril Aleksandrov
Kiril Aleksandrov

Reputation: 2591

You can use this approach

final Notification notification = buildForegroundNotification();
final NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(Constants.FOREGROUND_ID, notification);

Upvotes: 1

Related Questions