关于ZAKER 融媒体解决方案 合作 加入

使用 FCM 通过 C# 发送推送到 Android ( Firebase 云消息 .

CocoaChina 09-18

我正在使用此代码通过 C# 和 GCM 发送通知消息 , 使用Winforms,Webforms 等等 . 现在我想发送到 FCM ( Firebase 云消息传递 ) . 我应该更新我的代码吗? :

public class AndroidGCMPushNotification{ public AndroidGCMPushNotification ( ) { // // TODO: Add constructor logic here // } public string SendNotification ( string deviceId, string message ) { string SERVER_API_KEY = "server api key"; var SENDER_ID = "application number"; var value = message; WebRequest tRequest; tRequest = WebRequest.Create ( "https://android.googleapis.com/gcm/send" ) ; tRequest.Method = "post"; tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8"; tRequest.Headers.Add ( string.Format ( "Authorization: key={0}", SERVER_API_KEY ) ) ; tRequest.Headers.Add ( string.Format ( "Sender: id={0}", SENDER_ID ) ) ; string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString ( ) + "®istration_id=" + deviceId + ""; Console.WriteLine ( postData ) ; Byte [ ] byteArray = Encoding.UTF8.GetBytes ( postData ) ; tRequest.ContentLength = byteArray.Length; 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 sResponseFromServer; }}

但 GCM 改为 FCM. 发送通知的代码是否相同?

哪里可以找到 SERVER_API_KEY?是同样的解决方案吗?

最佳答案

使用 c# 的 firebase 云消息传递:

工作所有 .net 平台 ( asp.net,.netmvc,.netcore )

WebRequest tRequest = WebRequest.Create ( "https://fcm.googleapis.com/fcm/send" ) ; tRequest.Method = "post"; //serverKey - Key from Firebase cloud messaging server tRequest.Headers.Add ( string.Format ( "Authorization: key={0}", "AIXXXXXX...." ) ) ; //Sender Id - From firebase project setting tRequest.Headers.Add ( string.Format ( "Sender: id={0}", "XXXXX.." ) ) ; tRequest.ContentType = "application/json"; var payload = new { to = "e8EHtMwqsZY:APA91bFUktufXdsDLdXXXXXX..........XXXXXXXXXXXXXX", priority = "high", content_available = true, notification = new { body = "Test", title = "Test", badge = 1 }, }; string postbody = JsonConvert.SerializeObject ( payload ) .ToString ( ) ; Byte [ ] byteArray = Encoding.UTF8.GetBytes ( postbody ) ; 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 ( ) ) { if ( dataStreamResponse != null ) using ( StreamReader tReader = new StreamReader ( dataStreamResponse ) ) { String sResponseFromServer = tReader.ReadToEnd ( ) ; //result.Response = sResponseFromServer; } } } }

以上内容由"CocoaChina"上传发布 查看原文
相关标签 c#android消息

觉得文章不错,微信扫描分享好友

扫码分享