我知道这个问题被问了很多次。所以在这里请耐心等待。
我正在尝试使用Xamarin Forms通过My Android/iOS客户端实现Azure通知中心。
对于Android来说,它运行正常,但对于iOS,我没有收到任何通知。
这是我为iOS所做的工作的清单
- 生成证书和配置文件
- 已向 MacOS 注册证书
- 已导出 p12 文件并在 Azure 中心导入
- 我还在Info.plist中添加了权限(后台远程通知(
- Entitlements.plist 文件中没有更改
- 我能够从 APN 服务获取设备令牌
- 获取令牌后,我可以注册到 Azure 中心
但是我无法收到任何通知,也没有调用任何函数(DidRegisterUserNotificationSettings,WillPresentNotification,ReceivedRemoteNotification,DidRegisterUserNotificationSettings,FailedToRegisterForRemoteNotifications,DidReceiveRemoteNotification)
但RegisteredForRemoteNotifications
被称为
我还测试了设备令牌,无论是否从中删除'<> '
,但没有成功。
可能是什么问题?
这是我的代码
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
GlobalVariables.Instance.PnsToken = deviceToken.Description
.Replace("<", string.Empty)
.Replace(">", string.Empty)
.Replace(" ", string.Empty)
.ToUpper();
RegisteredForHub(application, deviceToken);
}
public void RegisteredForHub(UIApplication application, NSData deviceToken)
{
string pnsHandle = deviceToken.Description
.Replace("<", string.Empty)
.Replace(">", string.Empty)
.Replace(" ", string.Empty)
.ToUpper();
_hub = new SBNotificationHub(Constant.ListenConnectionString, Constant.NotificationHubName);
_hub.UnregisterNative((error) =>
{
if (error != null)
{
Debug.WriteLine($"Unable to call unregister Native {error}");
}
});
_hub.UnregisterTemplate("defaultTemplate",(error) =>
{
if (error != null)
{
Debug.WriteLine($"Unable to call unregister Template {error}");
}
});
// update registration with Azure Notification Hub
_hub.UnregisterAll(deviceToken, (error) =>
{
if (error != null)
{
Debug.WriteLine($"Unable to call unregister {error}");
return;
}
var tags = new NSSet(Constant.SubscribeTags);
_hub.RegisterNative(pnsHandle, tags, (errorCallback) =>
{
if (errorCallback != null)
{
Debug.WriteLine($"RegisterNativeAsync error: {errorCallback}");
}
});
var templateExpiration = DateTime.Now.AddDays(120)
.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
//_hub.RegisterTemplate(deviceToken, "defaultTemplate", Constant.ApnTemplateBody, templateExpiration,
// tags, (errorCallback) =>
// {
// if (errorCallback != null)
// {
// Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
// }
// });
});
经过大量的试验和错误以及这么多小时的谷歌搜索。我发现问题出在Azure上。
我上传了正确的证书,但密码是一个字符,因此没有发送通知。
如果用户为其证书文件输入了错误的密码,Azure 应提示用户输入 APN。