Cannot handle push notifications

Dmitriy Reznik 236 Reputation points
2020-12-10T14:46:48.77+00:00

I have a Xamarin.Forms app that uses push notifications. For Android, the following line:

(App.Current.MainPage as MainPage)?.AddMessage(body);

that is called from Android native OnMessageReceived(), throws NullReferenceException.

Why can this happen? Isn't App.Current to be accessible from the platform-specific project?

Here is the full OnMessageReceived() code:

    public override void OnMessageReceived(RemoteMessage message)
    {
        base.OnMessageReceived(message);
        string messageBody;

        if (message.GetNotification() != null)
        {
            messageBody = message.GetNotification().Body;
        }

        // NOTE: test messages sent via the Azure portal will be received here
        else
        {
            messageBody = message.Data.Values.First();
        }

        // convert the incoming message to a local notification
        SendLocalNotification(messageBody);

        // send the incoming message directly to the MainPage
        SendMessageToMainPage(messageBody); // THIS IS THE METHOD WHERE THE PROBLEMATIC LINE IS 
    }

I also tried to use MessagingCenter instead, like this:

        public override void OnMessageReceived(RemoteMessage message)
        {
            base.OnMessageReceived(message);
            string messageBody;

            if (message.GetNotification() != null)
            {
                messageBody = message.GetNotification().Body;
            }

            // NOTE: test messages sent via the Azure portal will be received here
            else
            {
                messageBody = message.Data.Values.First();
            }

            // convert the incoming message to a local notification
            SendLocalNotification(messageBody);

            // send the incoming message directly to the MainPage
            MessagingCenter.Send<object>(this, "newCall");

 //           SendMessageToMainPage(messageBody);
        }

But this message is never caught in the shared project's MainPage.xaml.cs.

Same issue with iOS, where I tried to send message from AppDelegate's DidReceiveRemoteNotification()...
Please help.

Azure Notification Hubs
Azure Notification Hubs
An Azure service that is used to send push notifications to all major platforms from the cloud or on-premises environments.
270 questions
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,299 questions
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2020-12-14T01:40:40.877+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The NullReferenceException shows App.Current.MainPage as MainPage is null in this scenario , please check if you set it correctly in App.cs .

    However I suggest you move the logic into App.cs, the class always exists and would never be null.

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful