Souvik Karmakar
Souvik Karmakar

Reputation: 79

NotificationCompat Builder error

I am facing an issue in building a Notification for my media player. Basically I am trying to build remote controls for my media player using Notification builder. I am using NotificationCompat Library to support lower version of android. Below is the part of the code which is facing error :

private NotificationCompat.Action createAction(int iconResId, String title, String action){
    Intent intent = new Intent(this, MusicBoxService.class);
    intent.setAction(action);
    PendingIntent pendingIntent = PendingIntent.getService(MainApplication.getContext(), 1, intent, 0);
    return new NotificationCompat.Action.Builder(iconResId, title, pendingIntent).build();
}

private void updateNotification(){
    NotificationCompat.Action playPauseAction = playbackState.getState() == playbackState.STATE_PLAYING ?
            createAction(R.drawable.ic_pause_black_18dp, "Pause", ACTION_PAUSE):
            createAction(R.drawable.ic_play_arrow_black_18dp, "Play", ACTION_PLAY);

    NotificationCompat notificationCompat = new NotificationCompat.Builder(this)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setCategory(NotificationCompat.CATEGORY_TRANSPORT)
            .setContentTitle(title)
            .setContentText(artist)
            .setOngoing(playbackState.getState() == playbackState.STATE_PLAYING)
            .setShowWhen(false)
            .setSmallIcon(R.drawable.ic_music_box_black_18dp)
            .setAutoCancel(false)
            .addAction(createAction(R.drawable.ic_skip_previous_black_18dp, "Previous", ACTION_PREV))
            .addAction(playPauseAction)
            .addAction(createAction(R.drawable.ic_skip_next_black_18dp, "Next", ACTION_NEXT))
            .setStyle(new NotificationCompat.MediaStyle().setMediaSession(mMediaSession.getSessionToken()).setShowActionsInCompactView(1,2))
                    .build();
    ((NotificationManagerCompat)getSystemService(Context.NOTIFICATION_SERVICE)).notify(1, notificationCompat);
}

Here is the screenshot of the error: enter image description here

The thing is that changing the line NotificationCompat notificationCompat = new ... to Notification notificationCompat = new .. solves the error, but I am not sure whether that is correct.

Android Studio gives the error: required: android.support.v7.app.NotificationCompat Found: android.app.Notification

Thanks! Any help is much appreciated.

Upvotes: 0

Views: 2864

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134714

NotificationCompat is just used for building the Notification. Building a notification using NotificationCompat.Builder still returns an android.app.Notification, not a NotificationCompat. Just change your declaration of notificationCompat to be:

Notification notification = new NotificationCompat.Builder(this)
        ...
        .build();   

Upvotes: 3

Related Questions