简体   繁体   中英

Xamarin.Forms - iOS RegisteredForRemoteNotifications not getting called

I am trying to register a physical iOS (13) device for remote notifications using Xamarin.iOS in a Xamarin.Forms project.

After it worked perfectly fine for about two weeks, I face the issue that after I call UIApplication.SharedApplication.RegisterForRemoteNotifications() (See complete code below)

Neither RegisteredForRemoteNotifications nor FailedToRegisterForRemoteNotifications of the AppDelegate is called.

The only thing that changed is that I added a production APNS-Certificate to my app-id that now looks like this: App id redacted with xxxx

I can select the associated development-provisioning profile in Xcode and I can deploy the app without errors.

The relevant code from the AppDelgate:

FinishedLaunching:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        Forms.SetFlags("IndicatorView_Experimental", "SwipeView_Experimental");

        Forms.Init();
        //other init methods omitted
        LoadApplication(new App());

        var baseFinished = base.FinishedLaunching(app, options);

        RegisterForRemoteNotifications();

        return baseFinished;
    }

RegisterForRemoteNotifications:

public void RegisterForRemoteNotifications()
    {
        if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
        {
            var options = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound |
                          UNAuthorizationOptions.Sound;

            UNUserNotificationCenter.Current.RequestAuthorization(options,(granted, error) =>
                {
                    if (granted)
                    {
                        //this method is getting called
                        InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);
                    }
                });
        }
        //checking for other iOS versions omitted
    }

RegisteredForRemoteNotifications and FailedToRegisterForRemoteNotifications - Both are not getting called:

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)

    {
        var bytes = deviceToken.ToArray();
        var token = BitConverter.ToString(bytes).Replace("-", "");
        Debug.WriteLine($"Got iOS notification token: {token}");

        DependencyService.Get<INotificationRegistrationService>().OnTokenAcquired(token, DeviceType.Apple);
    }

public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
    {
        Debug.WriteLine("error on register remote notifications ");
    }

I may have just made a mistake in the code or with the certificates.

For anyone with the same problem, try using a mobile hotspot / mobile internet to register the iPhone. This is what solved it for me

I guess my firewall somehow blocked communication with the apns server. I have no clue why because it worked before.

Now I will investigate how exactly the communication got blocked and if I have any relevant information, I will post them here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM