使用C#服务器将firebase云分散到多个用户(firebase cloud messging to multiple users with C# server)
我正在尝试使用我的c#服务器一次向多个电话发送FCM通知。 我可以使用以下代码一次向一部手机发送消息,
try { var applicationID = "xAxxxxxxxxxxxxxxxxxxxx6g9vOeEmw1njaivVfIx"; var senderId = "x7xxxxxxxxxxx5x"; string deviceId = pushToken; WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); tRequest.Method = "post"; tRequest.ContentType = "application/json"; var msg2send = new { to = deviceId, notification = new { body = msg, title = "My Car Wash App", icon = "myicon" }, data = new { priority = 10, notice = msg } }; var serializer = new JavaScriptSerializer(); var json = serializer.Serialize(msg2send); Response.Write(json); 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(); Response.Write(sResponseFromServer); } } } } } catch (Exception ex) { Response.Write(ex.Message); }
这是完美的,但我遍历我的标记,并一遍又一遍地调用该方法发出数百个请求,这并不好
我正在尝试发送多播消息,因为我可以传入一个包含所有令牌的数组,但我继续收到错误的请求
var msg2send = new { registration_ids = "[id1, id2]", notification = new { body = msg, title = "My Car Wash App", icon = "myicon" }, data = new { priority = 10, notice = msg } };
我很肯定,当我发送请求时,registration_ids是一个带有有效令牌的正确数组。 我不确定我做错了什么。 任何帮助或建议将非常感激。
I am trying to send FCM notifications to multiple phones at once with my c# server. I can send messages to one phone at a time with the following code,
try { var applicationID = "xAxxxxxxxxxxxxxxxxxxxx6g9vOeEmw1njaivVfIx"; var senderId = "x7xxxxxxxxxxx5x"; string deviceId = pushToken; WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); tRequest.Method = "post"; tRequest.ContentType = "application/json"; var msg2send = new { to = deviceId, notification = new { body = msg, title = "My Car Wash App", icon = "myicon" }, data = new { priority = 10, notice = msg } }; var serializer = new JavaScriptSerializer(); var json = serializer.Serialize(msg2send); Response.Write(json); 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(); Response.Write(sResponseFromServer); } } } } } catch (Exception ex) { Response.Write(ex.Message); }
that works perfectly but I loop through my tokens and call that method over and over making hundreds of requests and that's not good
I am trying to send a multi cast message as I can just pass in an array with all the tokens but I keep on getting bad request
var msg2send = new { registration_ids = "[id1, id2]", notification = new { body = msg, title = "My Car Wash App", icon = "myicon" }, data = new { priority = 10, notice = msg } };
I am positive that registration_ids is a correct array with valid tokens when I send my request. I am not sure what I am doing wrong. Any help or suggestions would really be greatly appreciated.
原文:https://stackoverflow.com/questions/39663466
最满意答案
Go-logging支持不同的日志记录后端,如文件,系统日志等。可以为每个后端和日志记录设置不同的日志级别。 例子在这里 。
Lumberjack也可以用于将日志写入滚动文件。 这是一个例子 。
Go-logging supports different logging backends like file, syslog etc. Multiple backends can be set with different log levels per backend and logger. Example over here.
Lumberjack can also be used with this for writing logs to rolling files. Here is an example.
相关问答
更多-
logging.getLogger()已经是单例。 ( 文件 ) 问题是每次调用myLogger() ,都会向该实例添加另一个处理程序,这会导致重复的日志。 也许这样的事情? import os import time import datetime import logging loggers = {} def myLogger(name): global loggers if loggers.get(name): return loggers.get(name) ...
-
正如Sven Schoenung在评论中所说,这最好是在Gulp本身之外完成的。 实质上,当在终端中运行任务时,两个选项按预期工作: 要“静默”运行,即没有控制台输出: gulp task-name > output.log 2>&1 要“正常”运行,即使用控制台输出: gulp task-name 2>&1 | tee output.log 两者都将控制台的输出保存到Gulp工作目录的根目录中的output.log 。 As Sven Schoenung suggests in comments, thi ...
-
nodejs记录到多个文件(nodejs logging to multiple files)[2022-05-13]
预期的行为是发送到stderr将被重定向到app.err ,而发送到stdout将进入app.out 。 例如这个脚本: var winston = require('winston'); var transports = [ new winston.transports.Console({ level: 'debug', handleExceptions: true, json: false, timestamp: () => (ne ... -
windows任务调度程序的工作目录在哪里? 我想你会在那里找到你的output1.txt; 或者尝试call "D:\test1.bat" >> D:\output1.txt 2>>&1 where is the working directory of the windows task scheduler? i think you will find your output1.txt there; or try call "D:\test1.bat" >> D:\output1.txt 2>>&1
-
如何使用NLog登录到具有不同日志记录级别的多个目标?(How to log to multiple targets with different logging levels using NLog?)[2023-06-27]
我刚刚找到了以编程方式记录多个目标的解决方案。 我只是不使用SimpleConfigurator而是使用LogManager将NLog.LoggingConfiguration设置为其属性。 所以我出来了以下代码片段: // define the targets // ...... // create configuration object and set previously created targets LoggingConfiguration configuration = new Loggin ... -
Expect的日志记录支持(即, log_file命令控制的内容)不允许您为不同的spawn ID设置不同的日志记录目标。 这意味着执行所需操作的最简单机制是在单独的进程中运行每个期望会话,如果不使用interact命令,这应该不会太难。 (需要同时与多个远程会话进行交互的想法有点奇怪!当你通过嫁接屏幕程序等方式使其变得合理时,你也可能会使用单独的期望脚本。) 在最简单的情况下,您的外部脚本可以只是: foreach host {foo.example.com bar.example.com grill.e ...
-
记录到多个输出(Go logging to multiple output)[2023-04-25]
Go-logging支持不同的日志记录后端,如文件,系统日志等。可以为每个后端和日志记录设置不同的日志级别。 例子在这里 。 Lumberjack也可以用于将日志写入滚动文件。 这是一个例子 。 Go-logging supports different logging backends like file, syslog etc. Multiple backends can be set with different log levels per backend and logger. Example o ... -
Python记录多个文件(Python Logging Multiple Files)[2022-05-14]
logging.getLogger([name])总是返回相同的对象(全局对象),每次调用runMain时都会在其上调用addHandler。 logging.getLogger([name]) always returns the same object (a global object) and you call addHandler on it each time you call runMain. -
记录多个模块(Logging over multiple modules)[2022-02-04]
设置正确的日志记录级别(如果要获取级别为ERROR或更高级别的所有消息,则至少为ERROR)并添加处理程序以将所有消息写入文件。 有关更多详细信息, 请查看https://docs.python.org/2/howto/logging-cookbook.html 。 Set the correct logging level (at least ERROR if you want to get all messages with level ERROR or higher) and add a handle ... -
记录Nant输出(Logging Nant Output)[2021-10-25]
使用target :: get-current-target函数。 Use the target::get-current-target function.