private Notification notification;
private final void createNotification(String action, Music music) {
int pausePlay = R.drawable.ic_baseline_play_arrow_24;
if (action.equals("play")) {
pausePlay = R.drawable.ic_baseline_pause_24;
}
Intent intent1 = new Intent(this, BroadcastClass.class).setAction("com.flymusic.players.musicService.MusicNotification.pausePlay");
Intent intent2 = new Intent(this, BroadcastClass.class).setAction("com.flymusic.players.musicService.MusicNotification.previous");
Intent intent3 = new Intent(this, BroadcastClass.class).setAction("com.flymusic.players.musicService.MusicNotification.next");
Resources r = GlobalContext.getContext().getResources();
Bitmap bmp = BitmapFactory.decodeResource(r, R.drawable.logo3);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 1, intent1, 0);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 2, intent2, 0);
PendingIntent pendingIntent3 = PendingIntent.getBroadcast(this, 3, intent3, 0);
Intent activityIntent = new Intent(this, MusicPlayerActivity.class);
PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 2, activityIntent, 0);
notification = new NotificationCompat.Builder(this, "channel1")
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentText(music.getSinger())
.setContentTitle(music.getName())
.setContentIntent(activityPendingIntent)
.addAction(R.drawable.ic_baseline_skip_previous_24, "Previous", pendingIntent2)
.addAction(pausePlay, "PausePlay", pendingIntent1)
.addAction(R.drawable.ic_baseline_skip_next_24, "Next", pendingIntent3)
.setSmallIcon(R.drawable.ic_baseline_music_note_24)
.setLargeIcon(PublicModule.albumBitmap==null? bmp:PublicModule.albumBitmap)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setOnlyAlertOnce(true)
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
// For backwards compatibility with Android L and earlier.
.setShowCancelButton(true)
.setCancelButtonIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
this,
PlaybackStateCompat.ACTION_STOP)))
.setSubText("Album")
.build();
NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("channel1", "channel1", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(0, notification);
startForeground(0, notification);
//Toast.makeText(this, "hhhhhhhhh", Toast.LENGTH_SHORT).show();
//new androidx.media.app.NotificationCompat.MediaStyle().setShowActionsInCompactView(0, 1, 2)
;
notificationBroadcastReceiver = new NotificationBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.flymusic.players.musicService.MusicNotification.pausePlay");
intentFilter.addAction("com.flymusic.players.musicService.MusicNotification.previous");
intentFilter.addAction("com.flymusic.players.musicService.MusicNotification.next");
registerReceiver(notificationBroadcastReceiver, intentFilter);
}
class NotificationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case "com.flymusic.players.musicService.MusicNotification.pausePlay":
playerPresenter.playOrPause();
break;
case "com.flymusic.players.musicService.MusicNotification.previous":
playerPresenter.PREVSong();
break;
case "com.flymusic.players.musicService.MusicNotification.next":
playerPresenter.NextSong();
break;
}
}
}