[英]Android push notifications using GCM with ASP.NET only received by one device
根据Android版Google云端消息(GCM)简单教程 ,我创建了一个Android应用程序,用于使用GCM服务进行推送通知。 现在,我可以在设备或仿真器中成功获取通知,但是问题是我只能在一个设备(用于测试目的的设备)中获取通知,而在其他安装了应用程序的设备中却无法获取通知。
final String regId = GCMRegistrar.getRegistrationId(this);
使用此代码,我得到注册ID,这是我从logcat窗口复制的输出,并手动发送给我的.Net开发人员。 他在服务器端复制了该ID,因此对于该设备,我会收到通知。 请帮助我将该注册ID动态发送到服务器端(Asp.net服务器)。 这样他就可以将这些ID存储在数据库中,并将注册ID数组传递给Google GCM服务器。 我认为只有这样我们才能获得所有设备(已安装的应用程序)的通知。 因为现在在服务器端,我们以这种方式将数据传递到GCM服务器:
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + deviceIDs + "";
如果我的程序在某处出错,请纠正我。
基本上,您要做的是打开与Web服务器的数据连接,然后将gcmid与手机的其他一些识别因素(例如,用户ID或电话ID)一起发送给您, GCMIntentService类的OnRegistered方法。 我将表单发布请求发送到Web服务器。 在网络服务器上,您可能需要将信息存储在数据库中。 我将多维数组用于表单变量
public String formPost(String[][] Vars) {
String url = "put url of web server here";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
String tmp = null;
String rturn = null;
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
for (int i = 0; i < Vars.length; i++) {
nameValuePairs.add(new BasicNameValuePair(Vars[i][0], Vars[i][1]));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String ttmp = parseISToString(response.getEntity().getContent());
Log.i("test", ttmp);
if (ttmp.contains("Success")) {
rturn = ttmp;
Log.i("test", "Success:" + ttmp);
} else {
rturn = ttmp;
Log.i("test", "Fail:" + ttmp);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
} /*
* catch (RuntimeException e){ Context context =
* getApplicationContext(); CharSequence text =
* "There was an error try again later."; int duration =
* Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text,
* duration);toast.show(); Log.i("test",e.getMessage()); finish(); }
*/
return rturn;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.