0

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.

  • you need to use matching type arguments in `Send` and `Subscribe`. See the [docs](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center) – Jason Mar 02 '23 at 14:16
  • @Jason they are strings, on both send and subscribe, `data.Data.TryGetValue("aKey", out var someValue)` produces a string, as the `nameof(zzz)`, am I missing something here? – innsmouth_dweller Mar 02 '23 at 15:22
  • `Send` where T1 is the type of the sender, and T2 is the type of the parameter. `Subscribe` where T1,T2 match the types used by `Send` – Jason Mar 02 '23 at 15:28

0 Answers0