Пользовательский макет уведомлений с нажатием кнопки не работает в Android oreo - PullRequest
0 голосов
/ 15 ноября 2018

Я прошел ответы, связанные с этой проблемой, но не нашел соответствия своему случаю. У меня есть служба, работающая в фоновом режиме и воспроизводящая музыку, я могу управлять этой музыкой с помощью пользовательского макета уведомлений, который имеет одну кнопку,Я смог сделать это для Android API меньше, чем 26, но когда я пробую на Android API уровней 26 и выше, отображается уведомление, но нет дескриптора для нажатия кнопки, и он показывает только макет в качестве исходного XML, хотя я установил заголовоки сообщение прагматично, и я использую идентификатор канала, когда я запускаю приложение на устройстве ниже Oreo, код работает должным образом, в Oreo отображается уведомление, но кнопка вообще не активна, а текст, отображающий заголовок и описание, не изменяется, пожалуйста.помогите заранее спасибо, вот мой код

 static RemoteViews contentView = null;
 static NotificationCompat.Builder mBuilder;
 static NotificationManager notificationManager;
 static Notification notification;



public void showCustomNotification(){
     String id = "id_product";
notificationManager= 

 (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= 26) {
    Log.d("rashed","it is oreo");
        // The user-visible name of the channel.
        CharSequence name = "Product";
        // The user-visible description of the channel.
        String description = "Notifications regarding our products";
        int importance = NotificationManager.IMPORTANCE_MAX;
        NotificationChannel mChannel = new NotificationChannel(id, name, 
         importance);
        // Configure the notification channel.
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        // Sets the notification light color for notifications posted to 
        this
        // channel, if the device supports this feature.
        mChannel.setLightColor(Color.RED);
        notificationManager.createNotificationChannel(mChannel);

        //
    }

    Intent closeButton = new Intent();

    closeButton.setAction(pauseAction);
    //  closeButton.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, 
    closeButton, 0);

    //sendBroadcast(closeButton);
    mBuilder = new NotificationCompat.Builder(this );
    mBuilder.addAction(R.drawable.exo_controls_pause, "", 
    pendingSwitchIntent);


    //   notificationManager = NotificationManagerCompat.from(this);
  //mBuilder.setContentIntent(pendingSwitchIntent);
 // notificationId is a unique int for each notification that you must 



    //
    contentbigView = new RemoteViews(getPackageName(), 
     R.layout.custom_push);
    contentView = new RemoteViews(getPackageName(), R.layout.custom_push);
    contentView.setImageViewResource(R.id.image, 
    R.drawable.ic_nav_about_active);
    contentView.setTextViewText(R.id.title, "radio FM");
    contentView.setTextViewText(R.id.text, "This is a custom layout");

    if (isPlay) {
        contentView.setOnClickPendingIntent(R.id.btn_close, 
       pendingSwitchIntent);

    } else {
        contentView.setImageViewResource(R.id.btn_close, 
       R.drawable.exo_controls_fastforward);
        contentView.setOnClickPendingIntent(R.id.btn_close, 
      pendingSwitchIntent);
    }


  //mBuilder.setCustomContentView(contentView);

    String chanel_id ="1";
    mBuilder.setSmallIcon(R.drawable.notification_icon)

   .setContent(contentView).setCustomBigContentView(contentbigView);


    notification = mBuilder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
      notificationManager.notify(1, notification);

вот мой пользовательский макет для

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/layout"
       android:layout_width="fill_parent"
       android:layout_height="64dp"
       android:padding="10dp"
       android:background="@color/black"
        >
         <ImageView
         android:src="@drawable/ic_nav_about_active"

             android:id="@+id/image"
            android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_alignParentLeft="true"
    />
       <TextView
    android:textSize="13dp"
    android:textColor="@color/colorred"
    android:text="Testing"
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/image"

    />
   <TextView
    android:textSize="13dp"
    android:textColor="@color/colorred"
    android:text="Testing is awecome"
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/image"
    android:layout_below="@id/title"
     />

    <ImageView
    android:id="@+id/btn_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="24dp"
      android:src="@drawable/exo_controls_pause"/>
        </RelativeLayout>
...