为 Firebase 通知创建消息头

Posted

技术标签:

【中文标题】为 Firebase 通知创建消息头【英文标题】:Creating a messagehead for a firebase notification 【发布时间】:2019-02-25 22:39:17 【问题描述】:

我正在尝试为 firebase 通知发送主要消息(例如 Massenger

但我的问题是应用程序启动时直接显示消息 没有收到任何消息。

我认为第一个问题的原因是我正在调用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();
            
        );

    

和 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】:

我首先从 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();
    

【讨论】:

以上是关于为 Firebase 通知创建消息头的主要内容,如果未能解决你的问题,请参考以下文章

Firebase 云消息传递为一组用户发送通知

Firebase 云消息传递 REST API Spring

如何在 Mule 中设置 ActiveMQ 消息头?

Firebase 控制台中的通知渠道不会将消息标记为“已打开”,即使消息已传递并打开也是如此

Web 应用程序中未收到 Firebase 云消息传递通知

使用 Firebase 的聊天应用程序:收到新消息时获取通知 - Android