I am trying to implement Push notifications for our Xamarin.iOS app. I've followed multiple Getting Started guides and other tutorials, but I can't seem to get a device token any more. It has worked before, and nothing significant changed, but I can't seem to pinpoint the problem here.
AppDelegate.cs
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate, IReceiverDelegate
{
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
var gcmConfig = Google.GoogleCloudMessaging.Config.DefaultConfig;
gcmConfig.ReceiverDelegate = this;
Google.GoogleCloudMessaging.Service.SharedInstance.Start(gcmConfig);
var notTypes = UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge;
var settings = UIUserNotificationSettings.GetSettingsForTypes(notTypes, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
// ... Other application stuff
return true;
}
public override void OnActivated(UIApplication application)
{
Google.GoogleCloudMessaging.Service.SharedInstance.Connect(error =>
{
if (error != null)
{
Console.WriteLine("GCM Connect error: " + error);
}
});
}
public override void DidEnterBackground(UIApplication application)
{
Google.GoogleCloudMessaging.Service.SharedInstance.Disconnect();
}
private NSData DeviceToken;
// This function does NOT get called anymore
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
this.DeviceToken = deviceToken;
var config = Google.InstanceID.Config.DefaultConfig;
Google.InstanceID.InstanceId.SharedInstance.Start(config);
var options = new NSMutableDictionary();
options.SetValueForKey(DeviceToken, Google.InstanceID.Constants.RegisterAPNSOption);
options.SetValueForKey(new NSNumber(true), Google.InstanceID.Constants.APNSServerTypeSandboxOption);
Google.InstanceID.InstanceId.SharedInstance.Token(
GCMConnection.SenderId,
Google.InstanceID.Constants.ScopeGCM,
options,
(token, error) => {
if (error == null)
{
// ... Send token to our API
PubSub.SharedInstance.Subscribe(token, "/topics/global", new NSDictionary(), delegate { });
return;
}
Console.WriteLine("Error getting GCM token: " + error);
// Handle error
});
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
Console.WriteLine(error);
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
// Handle notification here
Google.GoogleCloudMessaging.Service.SharedInstance.AppDidReceiveMessage(userInfo);
}
[Export("didDeleteMessagesOnServer")]
public void DidDeleteMessagesOnServer()
{
// ...
}
[Export("didSendDataMessageWithID:")]
public void DidSendDataMessage(string messageID)
{
// ...
}
[Export("willSendDataMessageWithID:error:")]
public void WillSendDataMessage(string messageID, NSError error)
{
// ...
}
}
I've shortened the code on some points for brevity.
At some point yesterday, I was getting my tokens properly and could even recieve some of the notifications that I was supposed to get. Without changing anything to the FinishedLaunching method, it suddenly stopped working, without any feedback as to why.
When I tried to fix it, I noticed some errors in the Console.Log related to GCM, which I've since all fixed (for example, I forgot the GoogleService-Info.plist file), to the point no errors are shown in the console.
I've read multiple times to clean+rebuild my project/solution, which I've done a couple of times now, but to no avail.
I am truely clueless as to where I should continue to look for.