0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;

namespace AndroidPushNOtifcation
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            SendCommandToAndroidPhone("APA91bHTWs7e0t57Y3ymbM2VVZakZ7vUzmXa8BJZdGAr06HUV5mBMbgorZOKlAQsMCmlXqXh1rt9IugPqi6eZ6cwfvZR_oY2YM9YzQnR45jXOrR5gK4U8oObnDjnObJ6d0PGMDX1MqFj_ey6_HKOXjty28T4H6YY7Q", "Second Test");
        }


        public string SendCommandToAndroidPhone(String deviceID, string Mesaj)
        {
            String GoogleAppID = "AIzaSyCwElHkNZpY6P2RkIsbMQWxI1OgQnuv8_g";

            WebRequest tRequest;
            tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/x-www-form-urlencoded";
            tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));

            String collaspeKey = Guid.NewGuid().ToString("n");

            String postData = string.Format("registration_id={0}&data.payload={1}&collapse_key={2}", deviceID, Mesaj, collaspeKey);

            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            tRequest.ContentLength = byteArray.Length;

            try
            {
                Stream dataStream = tRequest.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();

                WebResponse tResponse = tRequest.GetResponse();

                dataStream = tResponse.GetResponseStream();

                StreamReader tReader = new StreamReader(dataStream);

                String sResponseFromServer = tReader.ReadToEnd();

                tReader.Close();
                dataStream.Close();
                tResponse.Close();

                return "Android : " + sResponseFromServer;
            }
            catch (Exception ex)
            {
                return "Android : " + ex.Message;
            }
        }


    }



}

此代码适用于单个设备 ID 但在多播发送通知中失败请帮助我如何在 android 中发送多播通知。我已经尝试了很多,并尝试找到我在 Java 代码中找到的如何发送多播发送通知,但不是在 c# 代码中,请帮助我如何更改它多播。

4

1 回答 1

0

我认为问题在于您使用的是纯文本 HTTP 而不是 JSON。

Google 在他们的文档 ( http://developer.android.com/google/gcm/server.html ) 中声明,通过使用纯文本 HTTP,您一次只能发送到一个设备。

您可以选择切换到 JSON,您可以在一次调用中使用一组注册 ID(最多 1000 个)。

例如:

{ "collapse_key": "score_update",
   "time_to_live": 108,
   "delay_while_idle": true,
   "data": {
       "score": "4 x 8",
       "time": "15:16.2342"
   },
   "registration_ids":["4", "8", "15", "16", "23", "42"]
}

谷歌文档:http: //developer.android.com/training/cloudsync/gcm.html

编辑:这只是一个例子,我没有运行它,它来自我的记忆。但是你可以得到这个想法:(你可以使用http://json.codeplex.com/进行 JSON 转换):

try
{
    IEnumerable<string> registrationdIds = new List<string>(){"reg1","reg2","reg3"....,"reg1000"};
    var webRequest = WebRequest.Create("GCMServerUrl");
    webRequest.Headers.Add(HttpRequestHeader.Authorization, string.Format("Key={0}", "GCMApiKey"));
    webRequest.ContentType = "application/json";
    webRequest.Method = "POST";
    var requestStream = webRequest.GetRequestStream();

    var message = new Dictionary<string, object>();
    var data = new Dictionary<string, object>();
    var parameters = new NameValueCollection();

    var registrationIds = UserManagement.GetDefaultUserManagement().GetUserAndroidRegistrationIds();
    message.Add("registration_ids", registrationIds);
    data.Add("Field1", "This is Field1");
    data.Add("Field2", "This is Field2");
    message.Add("data", data);

    var jsonString = JsonConvert.SerializeObject(message);

    byte[] buffer = Encoding.UTF8.GetBytes(jsonString);
    requestStream.Write(buffer, 0, buffer.Length);

    using (var response = webRequest.GetResponse())
    {
        Logger.DebugFormat("Sent a GCM message. Response was:{0}", response);
    }
}
catch (Exception ex)
{
    Logger.ErrorFormat("Error occurred while sending GCM notification. Ex: {0}", ex);
}
于 2013-10-16T13:43:03.290 回答