我对此很陌生。 android的例子来自 GetStartedFirebase
以下是步骤:
1)我将android安装到手机
2)我按照这个例子来创建我的web api HTTPS // docs.microsoft.com / EN-US /天青/通知-集线器/通知-集线器-ASPNET-后端-GCM-机器人推到用户谷歌通知
3)我注释掉AuthenticationTestHandler类
4)我从小提琴中调用以下代码
DeviceRegistration对象
{
"Platform": "gcm",
"Handle": "regid i get from android",
"Tags": [
"1",
"2"
]
}
//这将创建或更新指定ID
的注册(使用提供的channelURI) public async Task<HttpResponseMessage> Put(string id, DeviceRegistration deviceUpdate)
{
RegistrationDescription registration = null;
switch (deviceUpdate.Platform)
{
case "mpns":
registration = new MpnsRegistrationDescription(deviceUpdate.Handle);
break;
case "wns":
registration = new WindowsRegistrationDescription(deviceUpdate.Handle);
break;
case "apns":
registration = new AppleRegistrationDescription(deviceUpdate.Handle);
break;
case "gcm":
registration = new GcmRegistrationDescription(deviceUpdate.Handle);
break;
default:
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
registration.RegistrationId = id;
var username = "test";
string[] userTag = new string[1];
userTag[0] = "username:" + username;
registration.Tags = new HashSet<string>(userTag);
try
{
await hub.CreateOrUpdateRegistrationAsync(registration);
}
catch (MessagingException e)
{
ReturnGoneIfHubResponseIsGone(e);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
5)然后我打电话发送推送通知
http://localhost:4486/api/Notifications?pns=gcm&to_tag=test
public async Task<HttpResponseMessage> Post(string pns, [FromBody]string message, string to_tag)
{
var user = "test";
message = "msg";
string[] userTag = new string[1];
userTag[0] = "username:" + to_tag;
Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
HttpStatusCode ret = HttpStatusCode.InternalServerError;
switch (pns.ToLower())
{
case "wns":
// Windows 8.1 / Windows Phone 8.1
var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">" +
"From " + user + ": " + message + "</text></binding></visual></toast>";
outcome = await Notifications.Instance.Hub.SendWindowsNativeNotificationAsync(toast, userTag);
break;
case "apns":
// iOS
var alert = "{\"aps\":{\"alert\":\"" + "From " + user + ": " + message + "\"}}";
outcome = await Notifications.Instance.Hub.SendAppleNativeNotificationAsync(alert, userTag);
break;
case "gcm":
// Android
var notif = "{ \"data\" : {\"message\":\"" + "From " + user + ": " + message + "\"}}";
outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif, userTag);
break;
}
if (outcome != null)
{
if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
(outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
{
ret = HttpStatusCode.OK;
}
}
return Request.CreateResponse(ret);
}
没有返回任何错误,但我没有收到任何通知。
我尝试删除usertag,如下所示:
outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif);
我可以收到通知。
为什么标签不起作用?
任何帮助表示感谢。
答案 0 :(得分:2)
答案 1 :(得分:0)
我很困惑 - 来自Fiddler的JSON表明注册有&#34; 1&#34;和&#34; 2&#34;标签,但你正在使用的代码中的任何地方&#34;用户名:test&#34;标签。你能从集线器获得这个注册并确保它有正确的标签吗?
您可以使用GetRegistrationAsync<TRegistrationDescription>(String)
[1],GetRegistrationsByChannelAsync(String, Int32)
[2]或GetAllRegistrationsAsync(Int32)
[3]方法进行注册。