Get data from Azure push notifications on Xamarin Forms page

I am developing a Xamarin Forms application that receives data from prestashop web services.

To make it work, you need to create an api key on the website and provide it to the application, but I think it will be more difficult for the average user to copy the 32-digit key from the Internet to the application.

Therefore, I decided that this is a good option for creating an azure application service firewall with a notification center, when the user creates an api key, he goes to xxx.azurewebsite.com, inserting the key, enter the client notification node identifier on the application login screen, and then A push notification sends it to the application, the api key is written to memory and the user logs in.

I follow this guide Add push notifications to your Xamarin.Forms application but I don’t see how I can get a notification on the current open screen and capture it in the ViewModel class in the general project.

+4
source share
1 answer

Here is a sample code that shows how to display a pop-up warning in Xamarin.Forms when you receive a push notification on any of the platforms.

Generic Xamarin.Forms Code

Create static classin the generic code Xamarin.Forms that runs DisplayAlert. Then create a method staticcalled ShowAlertthat can be accessed from our platform-oriented projects.

public static class PushNotificationHelpers
{
    public static async Task ShowAlert(string alert)
    {
        await Application.Current?.MainPage?.DisplayAlert("Push Notification Received", alert, "OK");
    }
}

iOS AppDelegate

Push Notification iOS, ReceivedRemoteNotification AppDelegate. ReceivedRemoteNotification , iOS push-.

[Register("AppDelegate")]
public partial class AppDelegate : FormsApplicationDelegate
{
    .
    .
    .

    public override async void ReceivedRemoteNotification(UIApplication app, NSDictionary userInfo)
    {
        // Process a notification received while the app was already open
        await ProcessNotification(userInfo);
    }

    async Task ProcessNotification(NSDictionary userInfo)
    {
        if (userInfo == null)
            return;

        Console.WriteLine("Received Notification");

        var apsKey = new NSString("aps");

        if (userInfo.ContainsKey(apsKey))
        {

            var alertKey = new NSString("alert");

            var aps = (NSDictionary)userInfo.ObjectForKey(apsKey);

            if (aps.ContainsKey(alertKey))
            {
                var alert = (NSString)aps.ObjectForKey(alertKey);

                await PushNotificationHelpers.ShowAlert(alert.ToString());

                Console.WriteLine("Notification: " + alert);
            }
        }
    }
}

Android GcmServiceBase

push- Android, , GcmServiceBase OnMessage. OnMessage , Android .

[Service(Name="com.sample.evolve.GcmService")] //Must use the service tag
public class GcmService : GcmServiceBase
{
    .
    .
    .

    protected override void OnMessage (Context context, Intent intent)
    {
        Console.WriteLine ("Received Notification");

        try
        {
            //Push Notification arrived - print out the keys/values
            if (intent != null || intent.Extras != null) 
            {

                var keyset = intent.Extras.KeySet ();

                foreach (var key in keyset)
                {
                    var message = intent.Extras.GetString(key);
                    Console.WriteLine("Key: {0}, Value: {1}", key, message);
                    if(key == "message")
                        await PushNotificationHelpers.ShowAlert(message);
                }
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine ("Error parsing message: " + ex);
        }
    }
}

Xamarin Evolve , Push- Xamarin.Forms. Xamarin Evolve, .

+2

Source: https://habr.com/ru/post/1665246/


All Articles