Change android notification text dynamically
Asked Answered
D

1

9

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.

Damaris answered 13/9, 2014 at 9:43 Comment(0)
G
14

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.

Grime answered 13/9, 2014 at 9:48 Comment(2)
Oh, now all documentations make sense. Thanks for pointing it out. I tried it and its working fine. I updated my question with solution.Damaris
Also please keep in mind if you want it to update pretty and not erase it and re-create (which will make your notification get erased and re-pop up somewhere else in the list), you NEED to use builder.setOnlyAlertOnce(true) as well as you MUST use the same builder, otherwise it wont update, it will do the goofy recreation thing. (I've been fighting with this for days and I still cannot seem to get it to update the buttons, it will only add them in addition to the existing ones :-( )Fragonard

© 2022 - 2024 — McMap. All rights reserved.