首页 \ 问答 \ 使用C#服务器将firebase云分散到多个用户(firebase cloud messging to multiple users with C# server)

使用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
更新时间:2023-02-02 17:02

最满意答案

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 ...
  • 预期的行为是发送到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
  • 我刚刚找到了以编程方式记录多个目标的解决方案。 我只是不使用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支持不同的日志记录后端,如文件,系统日志等。可以为每个后端和日志记录设置不同的日志级别。 例子在这里 。 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 ...
  • 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.
  • 设置正确的日志记录级别(如果要获取级别为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 ...
  • 使用target :: get-current-target函数。 Use the target::get-current-target function.

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。