【问题标题】:Android push notifications using GCM with ASP.NET only received by one device使用 GCM 和 ASP.NET 的 Android 推送通知仅由一台设备接收
【发布时间】:2013-01-10 13:15:17
【问题描述】:

根据Google Cloud Messaging For Android (GCM) Simple Tutorial,我创建了一个使用 GCM 服务推送通知的 android 应用程序。现在我成功地在设备或模拟器中收到通知,但问题是我只收到一个设备(用于测试目的的设备),而不是安装了其他应用程序的设备。

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() + "&registration_id=" + deviceIDs + "";

如果我的程序在某处出错,请纠正我。

【问题讨论】:

    标签: android asp.net notifications push-notification


    【解决方案1】:

    基本上,您要做的是打开到您的网络服务器的数据连接,并将您的 gcmid 与您想要做的电话的其他一些识别因素(例如用户 ID 或电话 ID)一起发送给它这在 GCMIntentService 类的 OnRegistered 方法中。我向网络服务器发送表单发布请求。在网络服务器上,您可能希望将信息存储在数据库中。 我对表单变量使用多维数组

    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;
    
    }
    

    【讨论】:

    • 感谢大卫的回复。有一些疑问。我在上面编辑的。
    • 你好 Zhydian,谢谢你的回复,还有其他的猎物,
    • 如果也清楚就好了,我已经把我的疑问作为另一个问题提出来了..请参考链接..stackoverflow.com/questions/14318115/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多