Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class QuizActivity extends AppCompatActivity implements View.OnClickListe
private static MediaSessionCompat mMediaSession;
private PlaybackStateCompat.Builder mStateBuilder;
private NotificationManager mNotificationManager;

private String CHANNEL_ID = "quiz_playback_channel"";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -190,15 +190,41 @@ private Button[] initializeButtons(ArrayList<Integer> answerSampleIDs) {
return buttons;
}

/**
*For android 8 above notification
*/
@RequiresApi(Build.VERSION_CODES.O)
private void createChannel() {
NotificationManager
mNotificationManager =
(NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = CHANNEL_ID;
// The user-visible name of the channel.
CharSequence name = "Media playback";
// The user-visible description of the channel.
String description = "Media playback controls";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.setShowBadge(false);
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mNotificationManager.createNotificationChannel(mChannel);
}

/**
* Shows Media Style notification, with actions that depend on the current MediaSession
* PlaybackState.
* @param state The PlaybackState of the MediaSession.
*/
private void showNotification(PlaybackStateCompat state) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);

int icon;
String play_pause;
if(state.getState() == PlaybackStateCompat.STATE_PLAYING){
Expand Down Expand Up @@ -230,7 +256,7 @@ private void showNotification(PlaybackStateCompat state) {
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.addAction(restartAction)
.addAction(playPauseAction)
.setStyle(new NotificationCompat.MediaStyle()
.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
.setMediaSession(mMediaSession.getSessionToken())
.setShowActionsInCompactView(0,1));

Expand Down