如何通过Windows Azure通过GCM发送特定Android设备的通知?

时间:2014-01-06 12:54:55

标签: android windows azure google-cloud-messaging

我已经通过GCM注册了Android设备,并且在Windows azure中注册了设备的注册ID,这为该设备提供了另一个唯一ID。现在我的疑问是如何通过windows azure(不是通过GCM注册ID)给出的注册ID向一个设备发送通知?我被引用了以下链接。http://msdn.microsoft.com/en-us/library/windowsazure/dn223273.aspx

在该链接中指定如下的URL。在这里,我知道名称空间,通知中心和消息,但我必须给出由windows azure给出的设备的唯一注册ID,以便向该设备发送通知

https://{namespace}.servicebus.windows.net/{NotificationHub}/messages/?api-version=2013-08

我在java中使用了以下代码

String strAzureURL = "https://namespaceName.servicebus.windows.net/hubName/testMessage";

                          String strAzureRegistrationId = "Tag1";

                    String strSharedAccessSignature = "MySharedAccessSignature";
                    String strSharedAccessName = "MySharedAccessName";
                    String strAuthorizationToken = URLDecoder.decode(strSharedAccessName+":"+strSharedAccessSignature,"UTF-8");

                    String strAuthWrapFormat = "WRAP access_token=\""+strAuthorizationToken+"\"";

                    String info = null;
                    HttpClient client = new HttpClient();
                    try {
                        GetMethod method = new GetMethod(strAzureURL);
                        method.setRequestHeader("Authorization", strAuthWrapFormat);

                        method.setRequestHeader("ServiceBusNotification-Tags", strAzureRegistrationId);
                        method.setRequestHeader("ServiceBusNotification-Format", "gcm");
                        method.setRequestHeader("Audience","https://namespaceName.servicebus.windows.net");
                        client.executeMethod(method);
                        info = method.getResponseBodyAsString();
                        System.out.println("Response "+info);
} catch (UnknownHostException e1) {
    e1.printStackTrace();
    throw e1;
}

现在我收到以下错误。

    Response <Error><Code>401</Code>

<Detail>MissingAudience: The provided token does not specify the 'Audience'..TrackingId:75c1d492-3ddf-4d88-84b7-1c2f07d9623b_G5,TimeStamp:1/8/2014 9:57:39 AM</Detail>

</Error>

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

查看此页面:http://msdn.microsoft.com/en-us/library/dn282661.aspx。它包括使用GCM注册客户端应用程序,然后使用通知中心的所有步骤。从客户端,它向Notification Hub注册,并将其“MyTag”设置为Tag。您要做的是使用设置为注册ID的标签进行注册。所以从客户端:

hub.register(registrationId, registrationId, <Optional Other Tags>);

然后,无论您何时触发推送通知,都可以将要传递的注册ID作为ServiceBusNotification-Tags传递(如您提供的REST API链接中所示)。然后,这将导致通知中心仅向已注册该标签的设备发送消息。