安卓如何自定义前台服务通知

5
如何自定义前台服务通知? 我尝试过:
private void startForegoundService() {
    RemoteViews remoteViews = new RemoteViews(getPackageName(),
            R.layout.custom_notification);
    Notification.Builder mBuilder = new Notification.Builder(this)
            .setContent(remoteViews);

    startForeground(mforegroundNotificationId, mBuilder.build());
}

R.layout.custom_notification是什么:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notification_layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#7777"
    android:padding="3dp" >

    <ImageView
        android:id="@+id/button_enable_alarm"
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:layout_marginRight="10dp"
        android:contentDescription="Eye"
        android:src="@drawable/action_enable_alarm" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        android:textSize="24sp" />

</LinearLayout>

但是它没有任何作用。我需要定制确切的前台服务通知,而不仅仅是服务的“假”通知。
1个回答

12

通过添加小图标价值来解决问题。完整代码:

Notification.Builder mBuilder = new Notification.Builder(this).setSmallIcon(R.drawable.custom_small_icon).setContent(remoteViews);

为什么setSmallIcon()控制自定义内容的可见性? - Sergei Bubenshchikov
@Nasib 你好!我没有在Android Pie上测试过它。 - Maxim Metelskiy
@XIII-th 我不知道为什么,但我刚刚发现了这个答案并使用了 setSmallIcon(),它也为我解决了问题。感谢楼主。 - Emad Omar
需要帮助 --> https://stackoverflow.com/questions/64984730/custom-notification-is-not-displaying-using-fcm - IRON MAN

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接