Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change android notification text dynamically

I am trying to make a notification for a music player with controls. I am successfully listening to the button click events and functions are being fired properly. Only problem i am facing is changing the text of notification on these click events. Here's what i am trying.

This is the receiver successfully receiving the calls and firing each and every line perfectly. But i cant the text changing. I think i have to reset the content view to Notification. If so, how do i do that?

@Override
public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
    if (action.equals("stop")) {
        ABCFragment.stopSong();
        Log.d("Notification","Stopping");
    }else if (action.equals("play")) {
        ABCFragment.togglePlayPause();
        Log.d("Notification","Toggle Play/Pause");
        RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.notification_layout);
        contentView.setTextViewText(R.id.songName, "SOME NEW SONG");
    }else if (action.equals("next")) {
        ABCFragment.playNextSong();
        Log.d("Notification","Next");
    }
}

Solution :

I updated my Notification class constructor to pass an extra arguments and got it working!

@Override
public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
    if (action.equals("stop")) {
        ABCFragment.stopSong();
        Log.d("Notification","Stopping");
    }else if (action.equals("play")) {
        ABCFragment.togglePlayPause();
        Log.d("Notification","Toggle Play/Pause");
        new ABCNotification(context, "SOME NEW SONG");
    }else if (action.equals("next")) {
        ABCFragment.playNextSong();
        Log.d("Notification","Next");
    }
}

constructor is handling the new passed arguments.

like image 461
Mercurial Avatar asked Sep 13 '14 09:09

Mercurial


1 Answers

you can't really change stuff on notification. It's a bit annoying for sure, you just have to replace the current notification with a new one with the new text.

My app there's an upload process and every 3 seconds we're updating the notification to change percentage.

so simply re-build the notification and call notify() on the NotificationManager with the same ID.

like image 160
Budius Avatar answered Sep 25 '22 01:09

Budius