Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
Posted
Updated 29-May-18 3:33am
Comments
Nelek 1-Jan-14 6:57am    
beniv 2-Jan-14 5:56am    
I'm looking for some got suugesstions

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.
 
Share this answer
 
v2
You can use firebase cloud messaging.

Following method, you can use.

C#
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);
    }
}
 
Share this answer
 
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($"Exception thrown in Notify Service: {ex}");
throw;
}

return false;
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900