且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#在Android中推送通知

更新时间:2023-02-27 08:01:30

你可以试试适用于Android的Google Cloud Messaging [ ^ ]。

android sdk\extras\google文件夹中有一个gcm示例。尝试一下。

简单来说,它的工作原理如下:

1.您学生的设备注册gcm服务器并获得注册ID。

2.将此注册保存在您的sqlserver中

3.要将消息发送到该学生的设备,请将带有该注册ID的消息发送到gcm服务器,然后gcm服务器将该消息推送到学生的设备。 / blockquote>
你可以使用firebase云消息。



以下方法,你可以使用。



 静态  void  SendMessage()
{
string serverKey = 您的服务器键跨度>;

尝试
{
var result =
- 1;
var webAddr = https:// fcm.googleapis.com/fcm/send跨度>;

var httpWebRequest =(HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = application / json;
httpWebRequest.Headers.Add( 授权:key = + serverKey);
httpWebRequest.Method = POST;

使用 var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = {\to\:\client Device token \,\data \:{\message \ :\这是Firebase云消息传递主题消息!\,}};
streamWriter.Write(json);
streamWriter.Flush();
}

var httpResponse =(HttpWebResponse)httpWebRequest.GetResponse();
使用 var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}

// 返回结果;
}
catch (例外情况)
{
// Response.Write(ex.Message);

}
}


使用FCM.Net;

使用Newtonsoft.Json;

使用System;

使用System.Collections.Generic;

使用System.Text;

使用System.Threading.Tasks;





使用System.Net;

使用System.Net.Http;

使用System.Net.Http.Headers;

使用System.IO;





亲爱的所有这是使用fcm.net核心和asp.net核心网络api的工作代码



公共异步任务< bool> NotifyAsync(string deviceToken,string serverKey,string body)

{



string json ={\to\:\\ \\+ deviceToken +\,\data \:{\message \:\+ body +\,}}};

尝试

{

// var result = - 1;

// var webAddr =https:// fcm。 googleapis.com/fcm/send;



// var httpWebRequest =(HttpWebRequest)WebRequest.Create(webAddr);

// httpWebRequest.ContentType =application / json;

//httpWebRequest.Headers.Add(\"Authorization:key=+ serverKey);

//httpWebRequest.Method = POST;



//使用(var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))

// {

//字符串json ={\to \: \+ deviceToken +\,\data \:{\message \:\这是Firebase云消息传递主题消息!\,}};

// streamWriter.Write(json);

// streamWriter.Flush();

//}



// var httpResponse =等待httpWebRequest.GetResponseAsync();

//使用(var streamReader = new StreamReader(httpResponse.GetResponseStream()))

// {

// result = streamReader.ReadToEnd();

//}



string Baseurl =https://fcm.googleapis.com/fcm/send;

var client = new HttpClient();

client.BaseAddress = new Uri(Baseurl);

client.DefaultRequestHeaders.Clear();



// var param = JsonConvert.SerializeObject(Myp roduct);

// string postBody = JsonConvert.SerializeObject(Myproduct);

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(application / json));

client.DefaultRequestHeaders.TryAddWithoutValidation(授权,key =+ serverKey);



HttpResponseMessage Res = await client.PostAsync( Baseurl,新的StringContent(json,Encoding.UTF8,application / json));



if(Res.IsSuccessStatusCode)

{

string returndata = await Res.Content.ReadAsStringAsync();

// Myproduct = JsonConvert.DeserializeObject< product>(returndata);

// System.Windows.Forms.MessageBox.Show(记录剃光);

返回true;

}



}

catch(Exception ex)

{

// _logger.LogError(


I have create a students information system In android. The Students have to login using his acounts create in SQLSERVER. wen he is successfull login the application display some latest news,etc. im geting the data from the sqlserver using wcf service.
but i'm stock in when i insert data for the stundet in sqlserver how to can i Inform the user like Notification (GCM) the student has some new news.

Sorry for my English

You can try Google Cloud Messaging for Android[^].
There is a gcm sample in the android sdk\extras\google folder. Try it out.
Briefly, it works like this:
1. Your student's device registers with gcm server and get a registraton id.
2. Save this registration in your sqlserver
3. To push message to this student's device, send the message with that registration id to the gcm server which will in turn pushes the message to the student's device.


You can use firebase cloud messaging.

Following method, you can use.

static void SendMessage()
{
    string serverKey = "Your server key";

    try
    {
        var result = "-1";
        var webAddr = "https://fcm.googleapis.com/fcm/send";

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Headers.Add("Authorization:key=" + serverKey);
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"to\": \"client Device token\",\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\",}}";
            streamWriter.Write(json);
            streamWriter.Flush();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            result = streamReader.ReadToEnd();
        }

       // return result;
    }
    catch (Exception ex)
    {
      //  Response.Write(ex.Message);
    }
}


using FCM.Net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;


using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.IO;


Dear All thisis working code using fcm.net core and asp.net core web api

public async Task<bool> NotifyAsync(string deviceToken, string serverKey, string body )
{

string json = "{\"to\": \"" + deviceToken + "\",\"data\": {\"message\": \"" + body + "\",}}";
try
{
//var result = "-1";
//var webAddr = "https://fcm.googleapis.com/fcm/send";

//var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
//httpWebRequest.ContentType = "application/json";
//httpWebRequest.Headers.Add("Authorization:key=" + serverKey);
//httpWebRequest.Method = "POST";

//using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
//{
// string json = "{\"to\": \"" + deviceToken + "\",\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\",}}";
// streamWriter.Write(json);
// streamWriter.Flush();
//}

//var httpResponse = await httpWebRequest.GetResponseAsync();
//using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
//{
// result = streamReader.ReadToEnd();
//}

string Baseurl = "https://fcm.googleapis.com/fcm/send";
var client = new HttpClient();
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();

//var param = JsonConvert.SerializeObject(Myproduct);
//string postBody = JsonConvert.SerializeObject(Myproduct);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "key="+ serverKey);

HttpResponseMessage Res = await client.PostAsync(Baseurl, new StringContent(json, Encoding.UTF8, "application/json"));

if (Res.IsSuccessStatusCode)
{
string returndata = await Res.Content.ReadAsStringAsync();
//Myproduct = JsonConvert.DeserializeObject<product>(returndata);
// System.Windows.Forms.MessageBox.Show("record shaved ");
return true;
}

}
catch (Exception ex)
{
// _logger.LogError(