使用C#中的FCM发送批量推送通知? [英] Sending bulk push notifications using FCM in C#?
问题描述
我正在编写一个c#服务,该服务将允许我使用FCM发送推送通知.现在,一切正常,但是我想使用FCM发送批量推送通知.
I am writing a c# service that will allow me to send push notifications using FCM. Now, everything works fine but then I want to send bulk push notification using FCM.
我希望能够一次将相同的消息发送到多个设备.当前使用一个注册令牌,然后完成整个过程,然后转到下一个.但是,如果我必须一次向多个电话发送推送通知,这将毫无用处.我最好的选择是什么?
I want to be able to send the same message to multiple devices at once. The current takes one registration token and then goes through the whole process and then goes to the next one. But this will be useless if I have to send push notification to many numbers at once. What should be my best option?
c#代码
public String SendPushNotification(string regtoken)
{
try
{
string applicationID = "#############3";
string senderId = "##########";
string deviceId = regtoken;
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
to = deviceId,
notification = new
{
body = message,
title = "",
sound = ""
}
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
tRequest.ContentLength = byteArray.Length;
using (Stream dataStream = tRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse tResponse = tRequest.GetResponse())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String sResponseFromServer = tReader.ReadToEnd();
String str = sResponseFromServer;
}
}
}
status = statusmessages();
}
}
catch (Exception ex)
{
string str = ex.Message;
status = "failure";
}
return status;
}
推荐答案
更新:对于v1,似乎不再支持registration_ids
,使用主题是最好的方法.
Update: For v1, it seems that registration_ids
is no longer supported and that making use of topics is the best approach.
您可以使用 registration_ids
参数,您最多可以在其中指定1000个注册令牌:
You can either use the registration_ids
parameter where you'll be able to specify up to 1000 registration tokens:
此参数指定多播消息的收件人,即发送到多个注册令牌的消息.
This parameter specifies the recipient of a multicast message, a message sent to more than one registration token.
该值应该是要向其发送多播消息的注册令牌的数组.该数组必须包含至少1个,最多1000个注册令牌.要将消息发送到单个设备,请使用to参数.
The value should be an array of registration tokens to which to send the multicast message. The array must contain at least 1 and at most 1000 registration tokens. To send a message to a single device, use the to parameter.
只需更换
to = deviceId
类似
registration_ids = deviceIds
其中deviceIds
是令牌数组.
或者您可以使用主题消息,只要用户订阅了您给定的主题,他们就会收到发送到该主题的消息.
Or you could make use of Topic Messaging, where so long as the user is subscribed to your given topic, they would receive messages sent to that topic.
这篇关于使用C#中的FCM发送批量推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!