Reputation: 424
I'm developing a Xamarin Forms app who gets data from prestashop webservices.
In order to make it work, you need to create a api key on the website and provide it to the app, but I think it will be a big problem to the average user to copy a 32 characters key from web to the app.
So I decided it's a good option to create a azure app service backend with a notification hub, when the user creates the api key, it goes to a xxx.azurewebsite.com paste the key, enter the customer notification hub id showed in the login screen of the app and then a push notification is send it to the app, the api key is captured stored and the user logs in.
I'm following this guide Add push notifications to your Xamarin.Forms app but I can't see how can I recive the notification in a currently open screen and capture it in a ViewModel class in the shared project.
Upvotes: 4
Views: 642
Reputation: 15340
Here's some sample code that shows how to display a pop-up alert in Xamarin.Forms when you receive a Push Notification on either platform.
Create a static class
in the Xamarin.Forms shared code that triggers DisplayAlert
. Then create a static
method called ShowAlert
that can be accessed from our platform-specific projects.
public static class PushNotificationHelpers
{
public static async Task ShowAlert(string alert)
{
await Application.Current?.MainPage?.DisplayAlert("Push Notification Received", alert, "OK");
}
}
To handle the Push Notification on iOS, you will override the ReceivedRemoteNotification
method in the AppDelegate
. The ReceivedRemoteNotification
method is triggered every time the iOS app receives a push notification.
[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);
}
}
}
}
To handle push notifications on Android, you will create a class that implements GcmServiceBase
and overrides OnMessage
. The OnMessage
method is triggered every time the Android App receives a notification.
[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);
}
}
}
The Xamarin Evolve app shows how to implement Push Notifications for a Xamarin.Forms app. I highly recommend perusing the Xamarin Evolve app source code to get a better understanding of the above sample code!
Upvotes: 3