I'm struggling to get push notifications working with Xamarin Forms and I'd really appreciate any help.
I've been following this tutorial, which itself appears to be out of date.
I'm specifically concerned with iOS at the moment. Here's what I've done:
I've setup an App Service in Azure and created a Notification Hub for it. I've created an APNS cert via my Apple Developer Centre and uploaded it to the Notification Hub as necessary for Apple push notifications. And I have the necessary provisioning profile certificate for signing the app to run on my device.
Here is the code in my AppDelegate class:
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init ();
var setup = new IOSAppContainerSetup();
LoadApplication (new App (setup));
// Register for push notifications.
var settings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert
| UIUserNotificationType.Badge
| UIUserNotificationType.Sound,
new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
return base.FinishedLaunching (app, options);
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
const string templateBodyAPNS = "{\"aps\":{\"alert\":\"$(messageParam)\"}}";
JObject templates = new JObject();
templates["genericMessage"] = new JObject
{
{"body", templateBodyAPNS}
};
// Register for push with your mobile app
Push push = AppContainer.MobileServiceClient.GetPush();
push.RegisterAsync(deviceToken, templates);
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
UIAlertView avAlert1 = new UIAlertView("Notification", "Got something", null, "OK", null);
avAlert1.Show();
NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
string alert = string.Empty;
if (aps.ContainsKey(new NSString("alert")))
alert = (aps[new NSString("alert")] as NSString).ToString();
//show alert
if (!string.IsNullOrEmpty(alert))
{
UIAlertView avAlert = new UIAlertView("Notification", alert, null, "OK", null);
avAlert.Show();
}
}
I suspect that the issue is with this line somewhere
Push push = AppContainer.MobileServiceClient.GetPush();
That line is basically a wrapper around this
var client = new MobileServiceClient(APIConstants.PUSH_NOTIFICATION_URL);
Which is done elsewhere in the app. I suspect I could be using the wrong URL, but nothing is obvious. I've tried the URL of the App Service and the Notification Hub endpoint, and in various shapes, i.e. dropping the protocol, dropping the trailing slashes...etc.
Nothing seems to work and I'm at a point where I'm tearing my hair out :)
Like I said, the tutorial seems to be out of date, and other tutorials that I've found are equally lacking. I'm testing this directly via the Test Send option under the Notification Hub via the Azure Portal, but nothing gets sent to the device, or at least the device isn't showing a notification.
Any help would be awesome, or if you've found a more useful tutorial online somewhere then please drop me a link.