Get data from Azure push notification on Xamarin Forms Page -
i'm developing xamarin forms app gets data prestashop webservices.
in order make work, need create api key on website , provide app, think big problem average user copy 32 characters key web app.
so decided it's option create azure app service backend notification hub, when user creates api key, goes xxx.azurewebsite.com paste key, enter customer notification hub id showed in login screen of app , push notification send app, api key captured stored , user logs in.
i'm following guide add push notifications xamarin.forms app can't see how can recive notification in open screen , capture in viewmodel class in shared project.
here's sample code shows how display pop-up alert in xamarin.forms when receive push notification on either platform.
xamarin.forms shared code
create static class
in xamarin.forms shared code triggers displayalert
. create static
method called showalert
can accessed 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"); } }
ios appdelegate
to handle push notification on ios, override receivedremotenotification
method in appdelegate
. receivedremotenotification
method triggered every time ios app receives push notification.
[register("appdelegate")] public partial class appdelegate : formsapplicationdelegate { . . . public override async void receivedremotenotification(uiapplication app, nsdictionary userinfo) { // process notification received while app 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
to handle push notifications on android, create class implements gcmservicebase
, overrides onmessage
. onmessage
method triggered every time android app receives notification.
[service(name="com.sample.evolve.gcmservice")] //must use service tag public class gcmservice : gcmservicebase { . . . protected override void onmessage (context context, intent intent) { console.writeline ("received notification"); try { //push notification arrived - print out 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); } } }
sample app
the xamarin evolve app shows how implement push notifications xamarin.forms app. highly recommend perusing xamarin evolve app source code better understanding of above sample code!
Comments
Post a Comment