Xamarin: shiny, push notification, and messaging center

337 views Asked by At

I'm using Shiny for Xamarin and, in the end, I want to use OnEntry method to navigate to a specific page, for now I'm content with displaying the alert only. I'm trying to use MessagingCenter but it doesn't work with background notifications.

public class MyPushDelegate : IPushDelegate
    {
        private readonly IMessagingCenter _messenger;

        public MyPushDelegate(IMessagingCenter messenger)
        {
            _messenger = messenger;
        }

        public Task OnEntry(PushNotification push)
        {
            data.Data.TryGetValue("aKey", out var someValue);
            _messenger.Send(nameof(App), "PushNotificationNavigationStarted", someValue ?? "There's no value :(");
            return Task.CompletedTask;
        }

        public Task OnReceived(PushNotification push)
            => Task.CompletedTask;

        public Task OnTokenRefreshed(string token)
            => Task.CompletedTask;
    }

This opens the app and displays the alert:

        public Task OnEntry(PushNotification push)
        {
            data.Data.TryGetValue("aKey", out var someValue);
            App.Current.MainPage.DisplayAlert("You've just received A VALUE!", someValue, "YAY");
            return Task.CompletedTask;
        }

The sample from Shiny project works nicely for a database insert.

I tried adding to AppStartUp (:ShinyStartup) the following method, which obviously didn't work

        private void SubscribeToPushNotificationClick()
        {
            MessagingCenter.Subscribe<string, string>(nameof(App), "PushNotificationNavigationStarted",
                async (_, someValue) => await App.DisplayAlert(someValue));
        }

And in App.xaml.cs:

        public static async Task DisplayAlert(string someValue)
        {
            await App.Current.MainPage.DisplayAlert("You've just received A VALUE!", someValue, "YAY");
        }

I want to navigate to a specific page after tapping the push notification (let's say someValue provides an id to retrieve some object from the API). But for now I'll be just glad to see a notification alert (not from OnEntry method). How to do this properly? Are there any navigation samples I haven't found? I'm absolutely new to both Shiny and push notifications.

I could use ISecureStorage to store the someValue from the delegate method, check if it exists on app load, navigate to a specific page, and clear it on page init, or in this simple example - display an alert and clear the value, but that seems like trying to scratch behind my ear with a foot, there has to be some simple and elegant way to do this.

0

There are 0 answers