首页 > 解决方案 > 使用自定义按钮组件创建通知

问题描述

我必须设计一个带有自定义布局和组件的通知,例如作为基本组件编写的按钮和 textview。我用 RemoteView 处理了自定义视图端。我创建了自定义布局,并在此布局中创建了 Textview 和 Button。问题是我必须使用之前在我的框架中编写的基本组件中的按钮和文本视图,但是当我尝试使用它时,它会出错。

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.n9020732.remoteview, PID: 2945
android.app.RemoteServiceException: Bad notification posted from package com.example.n9020732.remoteview: Couldn't inflate contentViewsandroid.view.InflateException: Binary XML file line #39: Binary XML file line #39: Error inflating class com.example.n9020732.remoteview.AXButton
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

我在此服务中创建通知。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("ONMESSAGERECEIVED", "Heloo");

    if (remoteMessage.getData().size() > 0) {

        RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.remote_view);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentText("Content of the notification")
                .setContentTitle("Title of the notification:")
                .setCustomBigContentView(expandedView);

        notificationManager.notify(0, builder.build());

    }
}

}

这是基础组件。

public class AButton extends 
android.support.v7.widget.AppCompatButton{
public AButton(Context context) {
    super(context);
}

public AButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public AButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

远程视图.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Content"/>

<TextView
    android:id="@+id/textview2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Description"/>

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher_foreground"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bas bana!"/>

    <com.example.n9020732.remoteview.AXButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Basma!"/>

</LinearLayout>

我如何在通知中使用我的基本组件。

标签: androidpush-notificationcomponentsbase

解决方案


推荐阅读