1

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.

  • Manually delete the app from your device/simulator and then allow it to be redeployed by xs/vs and retest – SushiHangover Jul 19 '16 at 14:36
  • Thanks for your suggestion, I've tried doing this but even doing this changed nothing. I get the screen at the startup of my app that "AppName wants to show you notifications" (or something along those lines), but neither `RegisteredForRemoteNotifications` nor `FailedToRegisterForRemoteNotifications` gets called. – Robby Groot Jul 19 '16 at 14:49
  • I think I found something related to this same issue, [right here](http://stackoverflow.com/a/38454917/6470327) – Robby Groot Jul 19 '16 at 15:13

2 Answers2

2

After wasting several hours trying to fix this issue today, I came across this answer, which quotes:

After a long dig I found that on 19 July, 2016 due to some error or updation at Apple's end , the didRegisterForRemoteNotificationsWithDeviceToken method would not be called even if every condition like Internet connection , Device and the methods used are perfect.

Refer to this link for confirmation https://forums.developer.apple.com/thread/52224

To verify please have a look in your other apps too. I had wasted my several hours but hope it helps someone. Thanks.

I guess I'll just have to wait until Apple solves this issue.


Edit on 20 July, 2016:
Without changing anything in my code, it started working again overnight. It's safe to assume that Apple has solved the issue.

Community
  • 1
  • 1
2

I had the same issue yesterday. Today I tried it again without any changes to my code and it worked. I spent about 5 hours in this issue and even searched the Apple developer sites to check if there were problems with Apple services and didn't find anything. Absolutely annoying!

In the end it seemed to be an apple-caused error that has been solved today.

toschu
  • 21
  • 2