单击按钮的自定义通知布局在Android Oreo中不起作用

时间:2018-11-15 08:50:56

标签: android notifications android-8.0-oreo custom-notificaiton

我查看了与该问题有关的答案,但没有找到与我的案例相符的答案。我在后台运行服务并播放音乐,我可以通过具有一个按钮的自定义通知布局来控制此音乐,我能够针对小于26的android API做到这一点,但是当我尝试使用26级及以上的android API时,会显示通知,但没有单击按钮的句柄,尽管我设置了标题,但它仅将布局显示为原始XML。和实用的消息,我使用频道ID,当我在Oreo下面的设备中运行该应用程序时,代码可以正常工作,在Oreo中显示了通知,但是按钮根本不可点击,文本视图的标题和描述都没有改变。 请提前帮助,谢谢,这是我的代码

L"123"

这是我自定义的

布局
 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);

0 个答案:

没有答案