使用Notifications REST API和JavaScript,我们通过FCM订阅了渐进式Web应用,然后调用registrations
端点在ANH上注册
注册完成正常,我们可以在具有正确平台和已填充PNS标识符的集线器上看到注册
当我们尝试向所有注册的设备发送测试消息时,我们在ANH中收到以下错误消息
从令牌提供者获得的令牌是错误的
我们尝试发送Firebase返回的整个endpoint
对象,仅发送subscriptionId
和其他各种组合
错误消息是否表示我们已使用错误的密钥对进行了订阅,或者令牌的格式不正确?没有地方显示GcmRegistrationId
格式向注册端点注册时的示例
答案 0 :(得分:0)
您可以使用以下方法注册设备:
如果是GCM,请遵循以下方法:
将Nuget包用于通知中心。
对于DeviceRegistration.cs
public class DeviceRegistration
{
public string Platform { get; set; }
public string Handle { get; set; }
public string[] Tags { get; set; }
}
对于NotificationClient.cs
using Microsoft.Azure.NotificationHubs; // Namespace to be used
// Use below method to get registrationID
public async Task<string> GetRegistrationID(NotificationHubClient Hub, string handle = null)
{
string newRegistrationId = null;
// make sure there are no existing registrations for this push handle (used for iOS and Android)
if (handle != null)
{
var registrations = await Hub.GetRegistrationsByChannelAsync(handle, 100);
foreach (RegistrationDescription registration in registrations)
{
if (newRegistrationId == null)
{
newRegistrationId = registration.RegistrationId;
}
else
{
await Hub.DeleteRegistrationAsync(registration);
}
}
}
if (newRegistrationId == null)
newRegistrationId = await Hub.CreateRegistrationIdAsync();
return newRegistrationId;
}
// Use below method to upsert registration to azure
public async Task UpsertRegistration(string registrationid, DeviceRegistration deviceUpdate, NotificationHubClient Hub)
{
string[] tags = { "abc","def" }; // These are used to send notifications
DeviceRegistration deviceRegistration = new DeviceRegistration
{
Handle = newDeviceToken, // Device token given by Firebase
Platform = "gcm", // Specify gcm for android and "apns" for ios
Tags = tags
};
RegistrationDescription registration
= new GcmRegistrationDescription(deviceRegistration.Handle);
registration.RegistrationId = registrationid;
// add check if user is allowed to add these tags
registration.Tags = new HashSet<string>();
foreach (string tag in deviceUpdate.Tags)
{
registration.Tags.Add(tag);
}
await Hub.CreateOrUpdateRegistrationAsync(registration);
}