Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change android notification message without stop and start service again

Tags:

android

This is my method for Notification when I start Service on android:

private void showNotification() {
        Notification notification = new Notification(R.drawable.call, "Some text", System.currentTimeMillis());
        Intent intent = new Intent(this, MainActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        notification.setLatestEventInfo(this, getString(R.string.notification_label), getString(R.string.notification_text_short), pi);
        notification.flags |= Notification.FLAG_NO_CLEAR;
        startForeground(7331, notification);
    }

is it possible to change text later. Now is for example "Some text" and later I would like to change it without stop and start service again.

Is that possible?

like image 865
senzacionale Avatar asked Dec 04 '12 07:12

senzacionale


1 Answers

You are using the notification ID 7331 for showing a notification as well as starting the servie. As long as you are sending a new notification with the same ID, it will replace the old one.

Use this to update the notification

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(ns);
long when = System.currentTimeMillis();

Notification notification = new Notification("your icon", "your ticker text", when);
/*<set your intents here>*/
mNotificationManager.notify(7331, notification);
like image 164
Kiran Kumar Avatar answered Sep 18 '22 11:09

Kiran Kumar