我正在尝试在Xamarin.Forms项目中使用Xamarin.iOS为远程通知注册物理iOS(13)设备。
[正常工作约两周后,我遇到了一个问题,UIApplication.SharedApplication.RegisterForRemoteNotifications()
(请参阅下面的完整代码)
AppDelegate的RegisteredForRemoteNotifications
和FailedToRegisterForRemoteNotifications
都不被调用。
唯一改变的是,我在我的应用程序ID中添加了生产APNS证书,现在看起来像这样:App id redacted with xxxx
我可以在Xcode中选择相关的开发配置文件,并且可以正确部署应用程序。
来自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和FailedToRegisterForRemoteNotifications-都没有被调用:
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 ");
}
我可能在代码或证书中犯了一个错误。
对于有相同问题的任何人,请尝试使用移动热点/移动互联网注册iPhone。这就是为我解决的问题
我猜我的防火墙以某种方式阻止了与APNS服务器的通信。我不知道为什么,因为它以前有用。
现在,我将调查通信被阻止的确切程度,如果有任何相关信息,请在此处发布。