Can’t receive Firebase data messages in background Xamarin.iOS app

Published

I have setup firebase cloud messaging in my Xamarin iOS app successfully. When I send messages via the firebase console, everything works fine.
On my backend, I added Firebase admin SDK for dotnet, and tried sending "data" messages to my iOS app in this way:

FIrst, using normal messages to topics:

var iOSMsg = new Message()
            {
                Topic = iOSDemoUsersTopic,
                Apns = new ApnsConfig
                {
                    Headers = new Dictionary<string, string>
                    {
                        { "priority", "high" }
                    },
                    Aps = new Aps
                    {
                        ContentAvailable = true,
                        MutableContent = true
                    }
                },
                Data = notificationData.ToDictionary(kv => kv.Key, kv => kv.Value.ToString())
            };
        var response = await FirebaseMessaging.DefaultInstance.SendAsync(message);

This approach didn’t work. Since I receive messages only when my app is open, when it goes I close it, notifications are not received anymore.

I then decided to try old Multicast messages, hopping it will work. I did as follows:

 var msg = new MulticastMessage
            {
                Apns = new ApnsConfig
                {
                    Headers = new Dictionary<string, string>
                    {
                        { "priority", "high" }
                    },
                    Aps = new Aps
                    {
                        ContentAvailable = true,
                        MutableContent = true,
                    }
                },
                Data = notificationData.ToDictionary(kv => kv.Key, kv => kv.Value.ToString()),
                Tokens = iOSTokens.ToList()
            };

            var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(msg);

But, the thing still behaves the same way.
On android, I can receive messages in background and when the app is killed when they are data messages. I have tried a million things, please can someone help ?

Source: Xamarin.ios Questions

Published
Categorised as Uncategorised Tagged , ,

Answers

Finally, I found a solution.
My notifications where sent well, but when gotten by iOS, the alert object of aps was not found, so the OS couldn’t display the notification.

To make this property available, the “Notification” property too should be set in the message.

  var msg = new Message
            {
                Apns = new ApnsConfig
                {
                    Aps = new Aps
                    {
                        ContentAvailable = true,
                        MutableContent = true,
                    }
                },
                Notification = new Notification
                {
                    Body = body,
                    Title = title
                },
                Data = notificationData.ToDictionary(kv => kv.Key, kv => kv.Value.ToString()),
                Topic = iOSDemoUsersTopic
            };

So, after adding the notification property, everything went fine.


Hilma Sporer

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq