如何通过GCM通过Windows Azure中发送一个通知特定的Andr​​oid设备? [英] How to send a notification for a particular android device by GCM through Windows azure?

查看:236
本文介绍了如何通过GCM通过Windows Azure中发送一个通知特定的Andr​​oid设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过设备的GCM和登记ID注册使用Windows Azure赋予了另一种独特的ID为设备注册的Andr​​oid设备。现在我的疑问是如何通过Windows Azure中给出(不GCM注册ID)的注册ID发送通知,只有一台设备?我提到下面的链接。 http://msdn.microsoft.com/ EN-US /库/ windowsazure / dn223273.aspx

URL指定的链接如下。在这里,我知道的命名空间,通知中心和消息,但在这里我必须给由Windows Azure的定器件的独特注册ID发送通知,该设备

<$p$p><$c$c>https://{namespace}.servicebus.windows.net/{NotificationHub}/messages/?api-version=2013-08

我用下面的code在Java

 字符串strAzureURL =htt​​ps://namespaceName.servicebus.windows.net/hubName/testMessage;

                          字符串strAzureRegistrationId =Tag1中;

                    字符串strSharedAccessSignature =MySharedAccessSignature;
                    字符串strSharedAccessName =MySharedAccessName;
                    字符串strAuthorizationToken = URLDe coder.de code(strSharedAccessName +:+ strSharedAccessSignature,UTF-8);

                    字符串strAuthWrapFormat =WRAP access_token = \+ strAuthorizationToken +\;

                    字符串信息= NULL;
                    HttpClient的客户端=新的HttpClient();
                    尝试 {
                        GetMethod的方法=新GetMethod的(strAzureURL);
                        method.setRequestHeader(授权,strAuthWrapFormat);

                        method.setRequestHeader(ServiceBusNotification-标签,strAzureRegistrationId);
                        method.setRequestHeader(ServiceBusNotification格式的,GCM);
                        method.setRequestHeader(读者,https://namespaceName.servicebus.windows.net);
                        client.executeMethod(法);
                        信息= method.getResponseBodyAsString();
                        的System.out.println(响应+信息);
}赶上(UnknownHostException异常E1){
    e1.printStackTrace();
    扔E1;
}
 

现在我收到以下错误。

 响应&LT;错误&GT;&LT; code&GT; 401&LT; / code取代;

&LT;详情&GT; MissingAudience:所提供的令牌不指定'观众'.. TrackingId:75c1d492-3ddf-4d88-84b7-1c2f07d9623b_G5,时间戳:2014年1月8日上午9时57分39秒&LT; /详情&GT;

&LT; /错误&GT;
 

感谢您的帮助!

解决方案

看看这个页面:的 http://msdn.microsoft.com/en-us/library/dn282661.aspx 。它包括所有的步骤,用于登记与GCM客户端应用程序,然后与通知集线器。从客户端,它与通知中心注册,并设置是它的MyTag的以成为标签。什么,你会想要做的是注册设置为注册ID的标签。所以从客户端:

  hub.register(registrationId,registrationId,&LT;可选配其他标签&GT;);
 

那么无论你正在触发推送通知,您会通过您要交付给作为ServiceBusNotification-标签(如您所提供的REST API链接看到)的注册ID。这将进而导致通知中心提供的消息,只已注册的标记设备。

I had registered android device by GCM and registration id of device is registered with Windows azure which gives an another unique id for that device. Now my doubt is how to send notification to only one device by the registration id given by windows azure(not by GCM registration id)? I was referred the following link.http://msdn.microsoft.com/en-us/library/windowsazure/dn223273.aspx

URL specified as follows in that link. Here i know namespace,Notification hub and messages but where i have to give that unique registration id of device given by windows azure for sending notification to that device

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

I had used the following code in 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;
}

Now i am receiving the following error.

    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>

Thanks for any help !

解决方案

Take a look at this page: http://msdn.microsoft.com/en-us/library/dn282661.aspx. It includes all of the steps for registering your client application with GCM and then with Notification Hubs. From the client side, it registers with the Notification Hub and set's it's "MyTag" to to be the Tag. What you'll want to do is register with a tag set to the registration ID. So from the client side:

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

Then from wherever you're triggering the push notification, you would pass in the registration ID you want to deliver to as the ServiceBusNotification-Tags (as seen in the REST API link you provided). That will then cause the Notification Hub to deliver a message to only devices that have registered with the tag.

这篇关于如何通过GCM通过Windows Azure中发送一个通知特定的Andr​​oid设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆