Firebase onMessageReceived not called when app in background

前端 未结 26 2657
粉色の甜心
粉色の甜心 2020-11-22 02:32

I\'m working with Firebase and testing sending notifications to my app from my server while the app is in the background. The notification is sent successfully, it even appe

26条回答
  •  情歌与酒
    2020-11-22 03:23

    Override the handleIntent Method of the FirebaseMessageService works for me.

    here the code in C# (Xamarin)

    public override void HandleIntent(Intent intent)
    {
        try
        {
            if (intent.Extras != null)
            {
                var builder = new RemoteMessage.Builder("MyFirebaseMessagingService");
    
                foreach (string key in intent.Extras.KeySet())
                {
                    builder.AddData(key, intent.Extras.Get(key).ToString());
                }
    
                this.OnMessageReceived(builder.Build());
            }
            else
            {
                base.HandleIntent(intent);
            }
        }
        catch (Exception)
        {
            base.HandleIntent(intent);
        }
    }
    

    and thats the Code in Java

    public void handleIntent(Intent intent)
    {
        try
        {
            if (intent.getExtras() != null)
            {
                RemoteMessage.Builder builder = new RemoteMessage.Builder("MyFirebaseMessagingService");
    
                for (String key : intent.getExtras().keySet())
                {
                    builder.addData(key, intent.getExtras().get(key).toString());
                }
    
                onMessageReceived(builder.build());
            }
            else
            {
                super.handleIntent(intent);
            }
        }
        catch (Exception e)
        {
            super.handleIntent(intent);
        }
    }
    

提交回复
热议问题