Нажатие кнопки прослушивания уведомлений
Я хочу указать различные действия при нажатии кнопки этого уведомления, которое я создаю. Например, пауза и следующая или предыдущая песня. И я хочу, чтобы текст песни был в панели уведомлений. Но это не показывается прямо сейчас. Я попробовал обычный способ, но это не сработало.
public void notifications() {
try {
CharSequence name = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_HIGH;
Intent notifyIntent = new Intent(this,AndroidBuildingMusicPlayerActivity.class);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent notificationIntent = new Intent(this, AndroidBuildingMusicPlayerActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
packageName = getPackageName();
// Setting our custom appearance for the notification
notificationView = new RemoteViews(packageName, R.layout.notification);
notificationView.setImageViewResource(R.id.notification_button_play, R.drawable.pause);
notificationView.setImageViewResource(R.id.notification_button_skip, R.drawable.skip);
notificationView.setTextViewText(R.id.notification_text_title,songTitle);
notificationView.setImageViewResource(R.id.notification_button_back, R.drawable.back);
Log.e("notification","title " + songTitle);
notificationBuilder = new Notification.Builder(this);
notificationBuilder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Music: Playing " + songTitle)
.setOngoing(true)
.setContentTitle("Music Player")
.setContentText(songTitle)
.setContent(notificationView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder.setChannelId(mChannelId).build();
mChannel = new NotificationChannel(mChannelId, name, importance);
}
Notification notification = notificationBuilder.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(mChannel);
}
manager.notify(0, notificationBuilder.build());
//Toast.makeText(this, "DONE", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("Error notification","e = "+e.toString());
}
}
Код для XML выглядит следующим образом: я прилагаю вывод макета уведомления: синий игрок мой
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/notification"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="@+id/notification_button_back"
android:layout_width="53dp"
android:layout_height="match_parent"
android:contentDescription="@string/notification_button_skip"
android:src="@drawable/back" />
<ImageButton
android:id="@+id/notification_button_play"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="@string/notification_button_play"
android:src="@drawable/play" />
<ImageButton
android:id="@+id/notification_button_skip"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="@string/notification_button_skip"
android:src="@drawable/skip" />
<TextView
android:id="@+id/notification_text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:singleLine="true"
android:text="placeholder text"
android:textStyle="bold" />