我正在开发一个 xamarin 应用程序,我需要在其中实现通知。我们决定使用 azure 来管理它们,但实际发送通知的是 firebase,因此设备需要在 NotificationHub 和 Firebase 服务中注册。
我已经成功直接在手机上执行此操作,使用WindowsAzure.Messaging组件和Xamarin - firebase messagesnuget。
但我在某处读到建议从应用程序后端执行此操作,所以我现在尝试像这样实现它。
所以我的new“后端”(实际上是一个wcf服务)是基于这个guide(我的ws的简化版本):
public async Task<string> RegistrerCampusHubs(Device device)
{
RegistrationDescription registration = null;
string newRegistrationId = device.Id;
try
{
if (device.Regitration.Handle != null)
{
var registrations = await hub.GetRegistrationsByChannelAsync(device.Regitration.Handle, 100);
foreach (RegistrationDescription reg in registrations)
{
if (newRegistrationId == null)
{
newRegistrationId = reg.RegistrationId;
}
else
{
await hub.DeleteRegistrationAsync(reg);
}
}
}
if (newRegistrationId == null)
{
newRegistrationId = await hub.CreateRegistrationIdAsync();
}
switch (device.Regitration.Platform)
{
case "mpns":
registration = new MpnsRegistrationDescription(device.Regitration.Handle);
break;
case "wns":
registration = new WindowsRegistrationDescription(device.Regitration.Handle);
break;
case "apns":
registration = new AppleRegistrationDescription(device.Regitration.Handle);
break;
case "gcm":
registration = new GcmRegistrationDescription(device.Regitration.Handle);
break;
}
registration.RegistrationId = newRegistrationId;
registration.Tags = new HashSet<string>(device.Regitration.Tags);
await hub.CreateOrUpdateRegistrationAsync(registration);
return "Success";
}
catch (Exception e)
{
return "Failed: " + e.Message;
}
}
这似乎有效,因为我可以在通知中心看到注册的设备,但是当我尝试发送消息时,我从 Visual Studio 中的通知中心测试发送者收到以下错误:
用于注册的推送通知系统句柄不再有效
我做了一些测试,得出的结论是问题是设备没有在 firebase 中注册。
await hub.CreateOrUpdateRegistrationAsync(registration);
这行代码在NotificationHub中注册设备,但不在firebase中注册。关于如何解决这个问题有什么想法吗?
根据您的描述,我按照本 tutorial 中的代码示例使用
NotificationHub.Register
和 Enable Firebase Cloud Messaging 来检查此问题。我可以通过 Visual Studio Server Explorer 使用 Test Send 和 Azure Portal 中的 Test Send 来接收通知。
用于注册的推送通知系统句柄不再有效根据您的情况,我假设您在Azure通知中心的注册可能无效(例如已过期)。我们建议您每次启动应用程序时都需要重新注册。此外,您可以参考此