如何使用多播从服务器端发送推送通知
本文关键字:通知 服务器端 何使用 多播 | 更新日期: 2023-09-27 18:35:37
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# 代码中,请帮助我如何更改它多播。
我认为问题是你使用的是纯文本HTTP而不是使用JSON。
谷歌在其文档(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);
}