Player com onGoingNotification

Asked

Viewed 46 times

1

I have the following code:

private Notification ongoingNotification() {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(getApplicationContext(), PlayerUI.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

    String title = "meuplayer";
    String msg = "Tocando. Clique para abrir.";

    if (currentStreamBeingPlayed != null) {
        msg = currentStreamBeingPlayed.getTitle();
    }

    PendingIntent previousSongPendingIntent = createPendingIntent(PlayerService.PREVIOUS_SONG);
    PendingIntent pausePendingIntent = createPendingIntent(PlayerService.PAUSE);
    PendingIntent nextSongPendingIntent = createPendingIntent(PlayerService.NEXT_SONG);
    PendingIntent playPendingIntent = createPendingIntent(PlayerService.PLAY);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.mipmap.ic_notification)
            .setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg)
            .addAction(R.mipmap.ic_prev, "", previousSongPendingIntent);
    if (!mediaPlayer.isPlaying()) {
        mBuilder.addAction(R.mipmap.ic_play, "", playPendingIntent);
    } else {
        mBuilder.addAction(R.mipmap.ic_pause, "", pausePendingIntent);
    }
    mBuilder.addAction(R.mipmap.ic_next, "", nextSongPendingIntent)
    .setOngoing(true);


    if (currentStreamBeingPlayed != null && currentStreamBeingPlayed.getPicture() != null) {
        try {
            URL urlPicture = new URL(currentStreamBeingPlayed.getPicture());
            Bitmap largeIcon = ImageHandler.decodeURLstreamToBitmap(urlPicture, getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width), getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height));
            mBuilder.setLargeIcon(largeIcon);
        } catch (MalformedURLException e) {
        } catch (Resources.NotFoundException e) {
        } catch (IOException e) {
        }
    }
    mBuilder.setContentIntent(contentIntent);
    return mBuilder.build();
}    

How can I make the image within the notification update along with the song (whether paused or in play)? because it is not giving an upgrade as the music runs

1 answer

2


Call the notify method of Notificationmanager, passing the same id as the previous notification that will update.

Notification mNotificationManager =  (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(seu_id, ongoingNotification())
  • I did this but either he gets stuck yet or he just doesn’t open the notification anymore

  • Not giving exception? I have similar codes working like this Alef, the difference is that I leave the Notificationmanager out of the method

  • but how do I make the Notificationmanager out, so it will give "java.lang.Nullpointerexception: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null Object Reference"

  • and then where can I put mNotificationManager.notify(seu_id, ongoingNotification())?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.