2

I'm trying to update a notification, But I'm not able to do it, Here's my code :

    NotificationCompat.Builder nBuilder;
RemoteViews remoteView;
static NotificationManager nManager;
static int NOFIY_ID = 2;
static Bitmap bitmap2;
static boolean isExists2;


public void MakeNotification(Context context,String Name,String AlbumART) {
    if(Added_RvHolders.isPlaying)
        Added_RvHolders.mMediaPlayer.stop();

    if(!AlbumART.equals("")) {
        Uri sArtworkUri = Uri
                .parse("content://media/external/audio/albumart");
        Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, Long.parseLong(AlbumART));
        try {
            bitmap2 = MediaStore.Images.Media.getBitmap(
                    context.getContentResolver(), albumArtUri);
            isExists2 = true;
        } catch (IOException e) {
            isExists2 = false;
        }
    } else {
        isExists2 = false;
    }

    if(IsFirstTime) {
        nBuilder = new NotificationCompat.Builder(context)
                .setAutoCancel(true)
                .setOnlyAlertOnce(true)
                .setSmallIcon(R.mipmap.small_app_icon)
                .setOngoing(true);
    remoteView = new RemoteViews(context.getPackageName(), R.layout.music_notification);
    }
    if(isExists2)
    remoteView.setImageViewBitmap(R.id.AlbumART, bitmap2);
    else
    remoteView.setImageViewResource(R.id.AlbumART,R.mipmap.main_app_icon);
    remoteView.setTextViewText(R.id.TrackTitle,Name);


    //set the button listeners
    setListeners(remoteView, context);
    nBuilder.setContent(remoteView);

    nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nManager.notify(NOFIY_ID, nBuilder.build());

}

What's wrong :

It's not being updated when choosing another song, in the 'OnClick' method, I'm passing the new name/cover art, But it's not updating, Even though I've tried to "cancel" the notification and make it again, Still on the same info .

What i'm expecting ? :

The notification gets updated when a new song is pressed .

curiousMind
  • 2,812
  • 1
  • 17
  • 38
Jaeger
  • 1,646
  • 8
  • 27
  • 59
  • Possible duplicate of [How to update Notification](http://stackoverflow.com/questions/8087310/how-to-update-notification) – Bö macht Blau Feb 23 '16 at 08:30
  • Nope, It's not a duplicate, because i'm doing what the answer says, and it stills not updated. – Jaeger Feb 24 '16 at 09:47
  • your code shows you are reusing an instance of NotificationCompat.Builder and creating a new instance of Notification. The answer to the other question suggested to use the *same instance of Notification*. So it seemed to me that this might me a duplicate. Please note the word "possible" in my comment :) – Bö macht Blau Feb 24 '16 at 10:05
  • I solved it, it was a problem from "static" used in the builder and the remote view, That's why it wasn't updated. – Jaeger Feb 24 '16 at 10:14
  • 1
    That's good to hear. So this is indeed no duplicate. Why don't you write your own answer? It could be useful to others with the same problem. – Bö macht Blau Feb 24 '16 at 10:24
  • 1
    You should probably not declare so many objects as static, perhaps revisit your design as this.. is questionable. – JoxTraex Feb 25 '16 at 03:38

1 Answers1

1

Beside using the same builder, You MUST NOT put the builder as static, or the remoteview, removing static solved my problem .

Jaeger
  • 1,646
  • 8
  • 27
  • 59
  • Just to avoid any misunderstanding (I'm no native speaker): it is ok to have the same builder to create first one notification and later the other, but I should not use static variables? – Bö macht Blau Feb 25 '16 at 06:51
  • Some variables need to declared as static, like the MediaPlayer, as some said "it will create instances", and if you try to stop & play another song using the MediaPlayer instance, it will resume the previous song and play the new one, to solve it you need to declare it as static ( check my previous question and you will understand ), I can't really answer you in Pro way, i'm still a beginner . – Jaeger Feb 25 '16 at 07:05
  • 2
    that's ok, I was just wondering about the meaning of "beside" - like I said, I'm no native speaker and one interpretation was that I may not ever reuse the builder, the other was like in my first comment. Translation probem, not coding problem ;) – Bö macht Blau Feb 25 '16 at 07:09
  • 1
    Ooh, for the "beside", the link you gave me suggests to use the same builder to solve the questioner problem, So "beside/with" that suggest, you also need to remove the static if found from the builder :) . – Jaeger Feb 25 '16 at 07:14