2017-02-27 52 views
1

我在应用程序收到fcm通知时显示自定义视图有困难。 我成功地显示收到的fcm通知给android的通知列表。 但我也希望fcm消息作为自定义视图显示在屏幕上(自上而下,几秒钟后消失),而不管应用状态如前景,背景或死亡。如何在应用程序收到fcm通知时显示自定义顶部/底部视图

我该如何解决我的问题? 先进的谢谢!

+0

尝试使用服务这一功能 –

+0

您是否尝试过'Notification.Builder'? –

回答

0

我猜你需要实现PopupWindow有在this doc

我要展示一个样品的情况下,这样你可以了解一个简短的想法。

假设你对你的Viewlayout是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> 

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

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()方法。 要知道如何实现其与Handler看到这个answer

从这个blog.

希望这有助于我得到的帮助!

+0

正如我所做的答案一样,但通知列表的视图已应用。那不是我想要的。我想要在屏幕上显示新的自定义视图,无论何时我使用其他应用程序或Android家庭等等。 – Dennis

+0

对不起,我的坏!但是现在我明白了你的观点。所以你想要一个弹出窗口来显示? –

+0

请检查我编辑的答案 –

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

正如我所做的答案一样,但通知列表的视图已应用。那不是我想要的。我想要在屏幕上显示新的自定义视图,无论何时我使用其他应用程序或Android家庭等等。 – Dennis

+0

根据我的理解,我觉得你想在收到通知后重定向到新的自定义视图?这是你想要实现的吗? –

+0

是的,如果应用程序收到通知,我希望通知最初显示在通知列表中,并且还希望通知在自定义视图的屏幕上显示。 – Dennis

相关问题