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

时间:2017-02-27 09:12:05

标签: android push-notification android-custom-view

当应用收到fcm通知时,我很难显示自定义视图。 我成功地将收到的fcm通知显示给android的通知列表。 但我也希望fcm消息在屏幕上显示为自定义视图(自上而下并在几秒钟后消失),无论应用程序状态如何,例如前景,背景或被杀死。

如何解决我的问题? 先谢谢你了!

2 个答案:

答案 0 :(得分:0)

我想你需要实施PopupWindowthis doc

中有一个简短的想法

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

假设layout的{​​{1}}是custom_layout.xml

View

然后,在您要处理云消息的位置,您只需添加此代码段

<?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>

在这种情况下,您只需点击一下即可关闭 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); // Inflate the custom layout/view View customView = inflater.inflate(R.layout.custom_layout,null); // Initialize a new instance of popup window mPopupWindow = new PopupWindow( customView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ); // Set an elevation value for popup window // Call requires API level 21 if(Build.VERSION.SDK_INT>=21){ mPopupWindow.setElevation(5.0f); } // Get a reference for the custom view close button ImageButton closeButton = (ImageButton) customView.findViewById(R.id.ib_close); // Set a click listener for the popup window close button closeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Dismiss the popup window mPopupWindow.dismiss(); } }); 。如果要自动关闭它,请使用mPopupWindow方法Handler。 要了解如何使用postDelayed()来实现它,请参阅此answer

我从blog.

获得了帮助

希望这有帮助!

答案 1 :(得分:0)

您可以使用以下代码制作自定义视图:

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