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.

Mercurial
  • 3,615
  • 5
  • 27
  • 52

1 Answers1

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.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Oh, now all documentations make sense. Thanks for pointing it out. I tried it and its working fine. I updated my question with solution. – Mercurial Sep 13 '14 at 10:23
  • 6
    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 :-( ) – Jared Sep 19 '14 at 01:19