Создание заголовка сообщения для уведомления о пожарной базе - PullRequest
0 голосов
/ 21 сентября 2018

Я пытаюсь создать головное сообщение (например, Massenger) для уведомлений firebase

Но моя проблема в том, что сообщение отображается непосредственно при запуске приложения без получения какого-либо сообщения.

Я думаю, что причина первой проблемы в том, что я вызываю код внутри oncreate, но я не добавляю представление, пока не отправлено сообщение.

Вот код

public class FireBaseMessagingService extends FirebaseMessagingService {


    FirebaseAuth mAuth;
    private WindowManager mWindowManager;
    private View mFloatingView;
    private CircleImageView mClose;
    private CircleImageView mImage;
    WindowManager.LayoutParams params;
    DatabaseReference mFriendDb;
    String myId = "";
    String FromId = "";
    boolean onCreate = true;
    @Override
    public void onCreate() {
        super.onCreate();


        //Inflate the floating view layout we created
        mFloatingView = LayoutInflater.from(this).inflate(R.layout.message_head, null);
        mAuth = FirebaseAuth.getInstance();
        myId = mAuth.getUid();
        mFriendDb = FirebaseDatabase.getInstance().getReference();
        mFriendDb.keepSynced(true);

        //Add the view to the window
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        mClose =  mFloatingView.findViewById(R.id.iv_message_head_close);
        mImage = mFloatingView.findViewById(R.id.iv_message_head_image);

        if (onCreate == false || !FromId.equals(myId))
        {
            addingView();
        }

        mClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeImage();
            }
        });

    }

and onMessageRecieve

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    onCreate = false;
    Map<String, String> data = remoteMessage.getData();
    FromId = data.get("From");
    String MessageType = data.get("Type");
    Log.i("MessageFrom" , "" + FromId);
    Log.i("MessageType","" + MessageType);
    mFriendDb.child(App_Constants.USERS_CELL).child(FromId).child(App_Constants.USER_INFO_CELL);
    mFriendDb.keepSynced(true);
    if (!FromId.equals(myId))
    {
        if (MessageType.equals(App_Constants.SENT_CELL))
        {
            mFriendDb.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    final String Image = dataSnapshot.child(App_Constants.USER_IMAGE_CELL).getValue().toString();
                    Picasso.get().load(Image).networkPolicy(NetworkPolicy.OFFLINE).into(mImage, new Callback() {
                        @Override
                        public void onSuccess() {
                        }

                        @Override
                        public void onError(Exception e) {
                            Picasso.get().load(Image).into(mImage);
                        }
                    });

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    }
    //Add the view to the window.

}

и метод добавления вида

private void addingView() {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
    }else{
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
    }


    //Specify the view position
    params.gravity = Gravity.TOP | Gravity.LEFT;        //Initially view will be added to top-left corner
    params.x = 0;
    params.y = 100;

    ringTone();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        if (!Settings.canDrawOverlays(this))
        {
            Intent myIntent = new Intent(this, InvisibleActivity.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(myIntent);
        }
        else
        {
            mWindowManager.addView(mFloatingView, params);
        }
    }

}

1 Ответ

0 голосов
/ 22 сентября 2018

я решил это, сначала удалив код из oncreate и сделав обработчик в методе onMessageReceived, и это полный код

public class FireBaseMessagingService extends FirebaseMessagingService {


    FirebaseAuth mAuth;
    private WindowManager mWindowManager;
    private View mFloatingView;
    private CircleImageView mClose;
    private CircleImageView mImage;
    WindowManager.LayoutParams params;
    DatabaseReference mFriendDb;
    String myId = "";
    String FromId = "";
    @Override
    public void onCreate() {
        super.onCreate();

        //Inflate the floating view layout we created
        mAuth = FirebaseAuth.getInstance();
        myId = mAuth.getUid();
        mFriendDb = FirebaseDatabase.getInstance().getReference();
        mFriendDb.keepSynced(true);

        //Add the view to the window
        mFloatingView = LayoutInflater.from(this).inflate(R.layout.message_head, null);
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        mClose =  mFloatingView.findViewById(R.id.iv_message_head_close);
        mImage = mFloatingView.findViewById(R.id.iv_message_head_image);



        mClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeImage();
            }
        });


    }


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Map<String, String> data = remoteMessage.getData();
        FromId = data.get("From");
        String MessageType = data.get("Type");
        Log.i("MessageFrom" , "" + FromId);
        Log.i("MessageType","" + MessageType);

        if (!FromId.equals(myId))
        {
            if (MessageType.equals(App_Constants.SENT_CELL))
            {
                mFriendDb.child(App_Constants.USERS_CELL).child(FromId).child(App_Constants.USER_INFO_CELL).
                        addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        final String Image = dataSnapshot.child(App_Constants.USER_IMAGE_CELL).getValue().toString();
                        if (Image != null)
                        {
                            Picasso.get().load(Image).networkPolicy(NetworkPolicy.OFFLINE).into(mImage, new Callback() {
                                @Override
                                public void onSuccess() {
                                }

                                @Override
                                public void onError(Exception e) {
                                    Picasso.get().load(Image).into(mImage);
                                }
                            });

                        }

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            }
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    addingView();
                }
            });
        }

        //Add the view to the window.

    }

    private void addingView() {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_PHONE,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);
        }else{
            params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);
        }


        //Specify the view position
        params.gravity = Gravity.TOP | Gravity.LEFT;        //Initially view will be added to top-left corner
        params.x = 0;
        params.y = 100;

        ringTone();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            if (!Settings.canDrawOverlays(this))
            {
                Log.i("IsGoing","Goind");
                Intent myIntent = new Intent(this, InvisibleActivity.class);
                myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(myIntent);
            }
            else
            {
                mWindowManager.addView(mFloatingView, params);
            }
        }

    }

    private void closeImage()
    {
        if (mFloatingView != null) mWindowManager.removeView(mFloatingView);
    }



    private void ringTone(){
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    }
}
...