Eric
Eric

Reputation: 107

Adding Notification Channels To Display Notification In Higher Android

I learned that notification channel is now needed in other to display notification to android devices running android OS Oreo or higher, but I'm still finding it difficult to understand the basics of adding channels in my notification services.
Like in my current media player project I want to display media control notification when song starts to play, so I followed a tutorial and was able to get the notification to appear but only in devices running OS 6.0 (which is the lowest OS I have tested with) and when I try to run in another device of OS 9.0 nothing appears.
I think it's because my channel has not been set up correctly.

this is the class that handles the notification

@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void handleIntent( Intent intent ) {
    if( intent == null || intent.getAction() == null )
        return;

    String action = intent.getAction();

    if( action.equalsIgnoreCase( ACTION_PLAY ) ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mController.getTransportControls().play();
        }
    } else if( action.equalsIgnoreCase( ACTION_PAUSE ) ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mController.getTransportControls().pause();
        }
    } else if( action.equalsIgnoreCase( ACTION_FAST_FORWARD ) ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mController.getTransportControls().fastForward();
        }
    } else if( action.equalsIgnoreCase( ACTION_REWIND ) ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mController.getTransportControls().rewind();
        }
    } else if( action.equalsIgnoreCase( ACTION_PREVIOUS ) ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mController.getTransportControls().skipToPrevious();
        }
    } else if( action.equalsIgnoreCase( ACTION_NEXT ) ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mController.getTransportControls().skipToNext();
        }
    } else if( action.equalsIgnoreCase( ACTION_STOP ) ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mController.getTransportControls().stop();
        }
    }
}

private Notification.Action generateAction(int icon, String title, String intentAction ) {
    Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
    intent.setAction( intentAction );
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return new Notification.Action.Builder( icon, title, pendingIntent ).build();
    }
    return null;
}

private void buildNotification( Notification.Action action ) {
    Notification.MediaStyle style = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        style = new Notification.MediaStyle();
    }

    Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
    intent.setAction( ACTION_STOP );
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
    Notification.Builder builder = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        builder = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.custom_launcher)
                .setContentTitle("Media Title")
                .setContentText("Media Artist")
                .setDeleteIntent(pendingIntent)
                .setStyle(style);

        NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        NotificationChannel channel = new NotificationChannel(channelID,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        if (mNotifyMgr != null) {
            mNotifyMgr.createNotificationChannel(channel);
        }

    }



    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        assert builder != null;
        builder.addAction( generateAction( android.R.drawable.ic_media_previous, "Previous", ACTION_PREVIOUS ) );
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.addAction( generateAction( android.R.drawable.ic_media_rew, "Rewind", ACTION_REWIND ) );
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.addAction( action );
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.addAction( generateAction( android.R.drawable.ic_media_ff, "Fast Foward", ACTION_FAST_FORWARD ) );
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.addAction( generateAction( android.R.drawable.ic_media_next, "Next", ACTION_NEXT ) );
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        style.setShowActionsInCompactView(0,1,2,3,4);
    }

    NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE );
    notificationManager.notify( 1, builder.build() );
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if( mManager == null ) {
        initMediaSessions();
    }

    handleIntent( intent );
    return super.onStartCommand(intent, flags, startId);
}

private void initMediaSessions() {
    mMediaPlayer = new MediaPlayer();


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mSession = new MediaSession(getApplicationContext(), "simple player session");
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mController =new MediaController(getApplicationContext(), mSession.getSessionToken());
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mSession.setCallback(new MediaSession.Callback(){
                                 @Override
                                 public void onPlay() {
                                     super.onPlay();
                                     Log.e( "MediaPlayerService", "onPlay");
                                     buildNotification( generateAction( android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE ) );
                                 }

                                 @Override
                                 public void onPause() {
                                     super.onPause();
                                     Log.e( "MediaPlayerService", "onPause");
                                     buildNotification(generateAction(android.R.drawable.ic_media_play, "Play", ACTION_PLAY));
                                 }

                                 @Override
                                 public void onSkipToNext() {
                                     super.onSkipToNext();
                                     Log.e( "MediaPlayerService", "onSkipToNext");
                                     //Change media here
                                     buildNotification( generateAction( android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE ) );
                                 }

                                 @Override
                                 public void onSkipToPrevious() {
                                     super.onSkipToPrevious();
                                     Log.e( "MediaPlayerService", "onSkipToPrevious");
                                     //Change media here
                                     buildNotification( generateAction( android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE ) );
                                 }

                                 @Override
                                 public void onFastForward() {
                                     super.onFastForward();
                                     Log.e( "MediaPlayerService", "onFastForward");
                                     //Manipulate current media here
                                 }

                                 @Override
                                 public void onRewind() {
                                     super.onRewind();
                                     Log.e( "MediaPlayerService", "onRewind");
                                     //Manipulate current media here
                                 }

                                 @Override
                                 public void onStop() {
                                     super.onStop();
                                     Log.e( "MediaPlayerService", "onStop");
                                     //Stop media player here
                                     NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                                     notificationManager.cancel( 1 );
                                     Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
                                     stopService( intent );
                                 }

                                 @Override
                                 public void onSeekTo(long pos) {
                                     super.onSeekTo(pos);
                                 }

                                 @Override
                                 public void onSetRating(Rating rating) {
                                     super.onSetRating(rating);
                                 }
                             }
        );
    }
}

@Override
public boolean onUnbind(Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mSession.release();
    }
    return super.onUnbind(intent);
}

Upvotes: 0

Views: 56

Answers (1)

No Body
No Body

Reputation: 681

When you want to create a NotificationBuilder, you have to pass the GroupId to it.

builder = new Notification.Builder(context, channelId)

Upvotes: 1

Related Questions