Android 当应用程序收到fcm通知时,如何显示自定义的上/下视图

Android 当应用程序收到fcm通知时,如何显示自定义的上/下视图,android,push-notification,android-custom-view,Android,Push Notification,Android Custom View,当应用程序收到fcm通知时,我很难显示自定义视图。 我成功地将收到的fcm通知显示到android的通知列表中。 但我也希望fcm消息在屏幕上显示为自定义视图(自上而下,几秒钟后消失),而不考虑应用程序状态,如前台、后台或已终止 我如何解决我的问题? 提前谢谢你 我想您需要实现PopupWindow对 我将演示一个示例案例,以便您能够理解 假设视图的布局是custom_layout.xml <?xml version="1.0" encoding="utf-8"?> <Rela

当应用程序收到fcm通知时,我很难显示自定义视图。 我成功地将收到的fcm通知显示到android的通知列表中。 但我也希望fcm消息在屏幕上显示为自定义视图(自上而下,几秒钟后消失),而不考虑应用程序状态,如前台、后台或已终止

我如何解决我的问题?
提前谢谢你

我想您需要实现
PopupWindow

我将演示一个示例案例,以便您能够理解

假设
视图的
布局
是custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_custom_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="2dp"
    android:background="#ab2fc4"
>
    <ImageButton
        android:id="@+id/ib_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_close_white_24dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="@null"
    />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a sample popup window."
        android:layout_centerInParent="true"
        android:padding="25sp"
    />
</RelativeLayout>
在这种情况下,只需单击即可关闭
窗口
。如果要自动关闭它,请使用其
postDelayed()
方法使用
Handler
。 要知道如何使用
处理程序实现它,请参见

我从这里得到了帮助


希望这有帮助

我想您需要实现
PopupWindow

我将演示一个示例案例,以便您能够理解

假设
视图的
布局
是custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_custom_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="2dp"
    android:background="#ab2fc4"
>
    <ImageButton
        android:id="@+id/ib_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_close_white_24dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="@null"
    />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a sample popup window."
        android:layout_centerInParent="true"
        android:padding="25sp"
    />
</RelativeLayout>
在这种情况下,只需单击即可关闭
窗口
。如果要自动关闭它,请使用其
postDelayed()
方法使用
Handler
。 要知道如何使用
处理程序实现它,请参见

我从这里得到了帮助


希望这有帮助

您可以使用以下代码创建自定义视图:

Notification notification = new Notification(icon, "Custom Notification", when);

        NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
        contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.title, "Custom notification");
        contentView.setTextViewText(R.id.text, "This is a custom layout");
        notification.contentView = contentView;

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;

        notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound

        mNotificationManager.notify(1, notification);

您可以使用以下代码创建自定义视图:

Notification notification = new Notification(icon, "Custom Notification", when);

        NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
        contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.title, "Custom notification");
        contentView.setTextViewText(R.id.text, "This is a custom layout");
        notification.contentView = contentView;

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;

        notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound

        mNotificationManager.notify(1, notification);


尝试使用此功能的服务您是否尝试过
通知.Builder
?尝试使用此功能的服务您是否尝试过
通知.Builder
?正如我所做的那样,您的回答是,但是,应用了通知列表视图。这不是我想要的。我希望新的自定义视图显示在屏幕上,每当我使用其他应用程序或android home等。哦,对不起,我的坏!但现在我明白你的意思了。所以你想弹出一个窗口吗?我想你的答案很好。所以我按照你们说的做了,但我得到了一个错误,“不能在并没有调用Looper.prepare()的线程内创建处理程序”。你们能看一下吗?另请参阅,希望能有所帮助……感谢您的评论。我决定部分申请提前通知。但我还有一个问题。如果我的应用程序未处于运行状态,则不会显示该消息。正如我所做的回答,但会应用通知列表视图。这不是我想要的。我希望新的自定义视图显示在屏幕上,每当我使用其他应用程序或android home等。哦,对不起,我的坏!但现在我明白你的意思了。所以你想弹出一个窗口吗?我想你的答案很好。所以我按照你们说的做了,但我得到了一个错误,“不能在并没有调用Looper.prepare()的线程内创建处理程序”。你们能看一下吗?另请参阅,希望能有所帮助……感谢您的评论。我决定部分申请提前通知。但我还有一个问题。如果我的应用程序未处于运行状态,则不会显示该消息。正如我所做的回答,但会应用通知列表视图。这不是我想要的。我希望每当我使用其他应用程序或android home等时,新的自定义视图都会显示在屏幕上。据我所知,我觉得你在收到通知后想重定向到新的自定义视图?这是您想要实现的吗?是的,如果应用程序收到通知,我希望通知最初显示在通知列表中,并且希望通知以自定义视图显示在屏幕上。好的。为此,您需要创建一个新类,并且您需要通过该类。在这门课上,你必须像我回答你的问题一样进行查看,但是,通知列表的视图是应用的。这不是我想要的。我希望每当我使用其他应用程序或android home等时,新的自定义视图都会显示在屏幕上。据我所知,我觉得你在收到通知后想重定向到新的自定义视图?这是您想要实现的吗?是的,如果应用程序收到通知,我希望通知最初显示在通知列表中,并且希望通知以自定义视图显示在屏幕上。好的。为此,您需要创建一个新类,并且您需要通过该类。在课堂上,你必须提出你的观点