0

I am have the following code that creates a notification for when the user starts to play music;

private Notification buildNotification(String title, String artist, Bitmap art) {
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
    notificationBuilder.setOngoing(true);
    notificationBuilder.setAutoCancel(true);  //TODO revert this to false depending on effect
    notificationBuilder.setSmallIcon(R.drawable.ic_stat_playing);

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1,
            new Intent(getApplicationContext(), MusicPlayer.class), PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.setContentIntent(pendingIntent);

    RemoteViews compactNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notfication_custom_layout);
    RemoteViews expandedNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_expanded_layout);

    Intent previousTrackIntent = new Intent(getApplicationContext(), MusicService.class);
    previousTrackIntent.setAction(MusicService.ACTION_REWIND);
    PendingIntent previousTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 1, previousTrackIntent, 0);

    Intent playPauseTrackIntent = new Intent(getApplicationContext(), MusicService.class);
    playPauseTrackIntent.setAction(MusicService.ACTION_TOGGLE_PLAYBACK);
    PendingIntent playPauseTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 1, playPauseTrackIntent, 0);

    Intent nextTrackIntent = new Intent(getApplicationContext(), MusicService.class);
    nextTrackIntent.setAction(MusicService.ACTION_SKIP);
    PendingIntent nextTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 1, nextTrackIntent, 0);

    Intent stopServiceIntent = new Intent(getApplicationContext(), MusicService.class);
    stopServiceIntent.setAction(MusicService.ACTION_STOP);
    PendingIntent stopServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, stopServiceIntent, 0);

    if (isPlayingMusic()) {
        compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_pause_dark);
        expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_pause_dark);
    } else {
        compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_play_dark);
        expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_play_dark);
    }

    compactNotification.setTextViewText(R.id.notification_base_line_one, title);
    compactNotification.setTextViewText(R.id.notification_base_line_two, artist);

    expandedNotification.setTextViewText(R.id.notification_expanded_base_line_one, title);
    expandedNotification.setTextViewText(R.id.notification_expanded_base_line_two, artist);

    compactNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
    expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_play, playPauseTrackPendingIntent);

    compactNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
    expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_next, nextTrackPendingIntent);

    compactNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
    expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_previous, previousTrackPendingIntent);

    compactNotification.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);
    expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_collapse, stopServicePendingIntent);

    expandedNotification.setImageViewBitmap(R.id.notification_expanded_base_image, art);
    compactNotification.setImageViewBitmap(R.id.notification_base_image, art);

    notificationBuilder.setContent(compactNotification);

    Notification notification = notificationBuilder.build();

    if (Build.VERSION.SDK_INT >= 16)
        notification.bigContentView = expandedNotification;

    notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_ONGOING_EVENT;

    return notification;
}

The notification appears but a few things have issues:

  • When the close button is pressed, the stopServiceIntent is called successfully and playback is stopped but the notification isn't cancelled.
  • When toggling playback from the player, the play button doesn't change according to the current state, i.e when paused, it should change to the play button and when playing, it should change to the pause button.

The code that should toggle the buttons is:

public boolean isPlayingMusic() {
    try {
        return mPlayer.isPlaying();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

The code that should cancel the notification when playback is stopped is:

private void cancelNotification() {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.cancel(NOTIFICATION_ID);
}
Ashish Shukla
  • 1,027
  • 16
  • 36
Alex Kombo
  • 3,256
  • 8
  • 34
  • 67
  • just put some log in this methods and check whether they are getting called or not. – karan Oct 01 '15 at 11:56
  • may be http://stackoverflow.com/a/24473506/1602333 can help with play/pause image resource issue – MohK Oct 01 '15 at 12:14

0 Answers0