I'm at the stage now where I'd like to add push notifications to my mobile app. I started by following the documentation on Xamarins official site (https://developer.xamarin.com/guides/xamarin-forms/cloud-services/push-notifications/azure/) and for the most part everything seemed to be just fine however I cannot recieve any push notifications.
- I can send a test push notification from my Azure Notification Hub which returns a `success` message but the phone does not show it.
- To test that my Azure notification hub was working I sent a test message from www.pushtry.com and that said the message was sent successfully but nothing arrived on my device. This ruled out Azure notification hub being at fault.
I need to understand where I have gone wrong in the process and what I can do to get it sorted out. Here is what I have done so far and the relative source code and my Apple Development Account settings.
Please note that I am testing my app with TestFlight just now meaning
I am still in the development stage.
Apple Developer Account
I have my Apple dev account set up as follows (taken from this documentation https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-ios-push-notification-apns-get-started).
- Certificate: *iOS Distribution / Expires 2018*
- App ID: *Set up to use push notifications with development SSL certificate for notifications*.
- Provisioning Profile: *ios distribution with push notifications*
I have also registered three devices using their UDID numbers.
Azure Portal
I have a notification hub setup in my Azure portal and the endpoints are registered in my client app code as well as it's name.
I uploaded my .p12 certificate as sandbox since that's how I set it up in the Apple Developer Portal and they must match from what I understand.
I can send a test push notification out and I get a success message back from the service in Azure but nothing arrives on the device.
Client App
To ensure I'm doing things correctly I have included my client app code taken from `AppDelegate.cs`. This code registers my app for notifications and also handles them when they are received.
using System;
using Foundation;
using UIKit;
using WindowsAzure.Messaging;
namespace MyApp.iOS
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register("AppDelegate")]
public partial class AppDelegate : Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
//Constants
public const string ConnectionString = "Endpoint=sb://xxxxxxxx.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=xxxx";
public const string NotificationHubPath = "mynotifications";
public SBNotificationHub hub { get; set; }
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication application, NSDictionary options)
{
//----------------------Notification setup Start-----------------------
//
//Push Settings including types of notifications this app can recieve.
var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert |
UIUserNotificationType.Badge |
UIUserNotificationType.Sound,
new NSSet());
application.RegisterUserNotificationSettings(pushSettings);
application.RegisterForRemoteNotifications();
//
//Just in case the notification is triggered here.
ProcessNotification(options);
//----------------------End-----------------------
//
// Declare Xamarin Forms
Xamarin.Forms.Forms.Init();
//
//Declare Azure Movile Services
Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
//
//Declare SQL Lite
SQLitePCL.Batteries.Init();
//
//Load Application
LoadApplication(new App());
return base.FinishedLaunching(application, options);
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
hub = new SBNotificationHub(ConnectionString, NotificationHubPath);
hub.UnregisterAllAsync(deviceToken, (error) =>
{
if (error != null)
{
Console.WriteLine($"Error unregistering: {error}");
}
hub.RegisterNativeAsync(deviceToken, null, registerError =>
{
if (registerError != null)
{
Console.WriteLine($"Error registering: {registerError}");
}
});
});
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
ProcessNotification(userInfo);
}
void ProcessNotification(NSDictionary options)
{
if (options == null)
{
return;
}
if (!options.ContainsKey(new NSString("aps")))
{
return;
}
var aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;
string alert = (aps[new NSString("alert")] as NSString).ToString();
var alertCtr = UIAlertController.Create("Received Message!", alert, UIAlertControllerStyle.Alert);
Window.RootViewController.ShowViewController(alertCtr, this);
}
}
}I feel like I'm working in the dark now, as far as I can see my app is set up correctly, my apple portal is setup correctly and my azure portal is too. Despite the success messages I just can't get notifications. I can only assume they are getting stuck at the APNS end but I have no way of knowing that and or debugging it.
I can only summise that something is wrong with my Apple Development portal profile and certificate combination but I'm not sure. I now need some help to try and get this working as it's been going on for weeks and I'm getting nowhere now.
Many thanks