1

I have a difficulty about showing custom view when app received fcm notification. I get in success to show received fcm notification to notification list of android. But I also want that fcm message to be shown on screen as a custom view(top down and disappear after a few seconds) regardless of app status, such as foreground, background or killed.

How can I resolve my problem? Thank you in advanced!

Dennis
  • 130
  • 10

2 Answers2

0

I guess you need to implement PopupWindow Have a brief idea in this doc

I am going to demonstrate a sample case so that you can understand.

Suppose your layout of your View is 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>

Then in where you are going to handle cloud message, you just add this code snippet

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

In this case you can close your mPopupWindow with a click. If you want to close it automatically, use a Handler using its postDelayed() method. To know how to implement it with a Handler see this answer

I got help from this blog.

Hope this helps!

Community
  • 1
  • 1
Tahmid Rahman
  • 748
  • 1
  • 8
  • 20
  • As I did your answer, but, view of notification list is applied. That's not it I want. I want new custom view to be shown on screen whenever I use some other app or android home and so on. – Dennis Feb 27 '17 at 10:25
  • Oh sorry, my bad! But now I got your point. So you want a pop-up window to show up? – Tahmid Rahman Feb 27 '17 at 10:34
  • I think your answer is good. So I did it as you say, But I get error, "Can't create handler inside thread that has not called Looper.prepare()". – Dennis Feb 28 '17 at 03:25
  • Would you please take a look at [here](http://stackoverflow.com/a/3875204/6155248)? See also [this](http://stackoverflow.com/a/25384688/6155248) Hope it helps... – Tahmid Rahman Feb 28 '17 at 05:18
  • Thanks for your comment. I soved it partially to apply head-up notification. But I have an other problem. That's not be shown if my app is not running state. – Dennis Feb 28 '17 at 07:52
  • Please share screen shot or any design to provide a visual demonstration so that the developers can understand and help you. Please clarify your problem in your question – Tahmid Rahman Feb 28 '17 at 08:03
  • I solved top/down notification problem as "head-up notification" of android if my app is in running, not popupwindow of your comment. But I have a new problem. That "head-up notification" view don't be shown if my app is not in running. – Dennis Feb 28 '17 at 08:36
0

You can use the code below to make custom view:

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);
  • As I did your answer, but, view of notification list is applied. That's not it I want. I want new custom view to be shown on screen whenever I use some other app or android home and so on. – Dennis Feb 27 '17 at 10:25
  • According to what I could understand, I feel you want to redirect to new custom view after receiving notification? Is it what you want to implement? – Deepali-Systematix Feb 27 '17 at 10:59
  • Yes, if app receive notification, I want notification to show at notification list originally and also want the notification to be shown on screen with custom view. – Dennis Feb 28 '17 at 03:22
  • okay. For that you need to make a new class and in intent you need to pass that class. and in that class you have to make your view – Deepali-Systematix Feb 28 '17 at 13:38