Click here to Skip to main content
14,926,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have create a wcf service to send notification to my android application but its not working

What I have tried:

my wcf method is


C#
public string SendNotification1(string serverApiKey, string senderId, string deviceId, string message)
        {

          
                string result;
                var value = message;



                WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                tRequest.Method = "post";
                tRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
                tRequest.Headers.Add(string.Format("Authorization: key={0}", serverApiKey));
                tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));

                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 + "";

                Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                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();
                                
                            }
                        }
                    }
                }
           

            return message;



        }


my iService:

C#
[OperationContract]
       [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "SendNotification?serverApiKey={serverApiKey}&senderId={senderId}&deviceId={deviceId}&message={message}")]

       string SendNotification1(string serverApiKey, string senderId, string deviceId, string message);


wen my android application start I use service to save device token

I send success full notification using PHP but I need it with wcf service
the php code is

PHP
<?php 

	function send_notification ($tokens, $message)
	{
		

		$url = 'https://fcm.googleapis.com/fcm/send';
		$fields = array(
			 'registration_ids' => $tokens,
			 'data' => $message
			);
	
			$headers = array(			
			'Authorization:key =mykeyyyyyyy',
			'Content-Type: application/json'
			);
			
			
	   $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
       $result = curl_exec($ch);           
       if ($result === FALSE) {
           die('Curl failed: ' . curl_error($ch));
       }
       curl_close($ch);
       return $result;
	}
	

	$conn = mysqli_connect("localhost","root","","fcm2");

	$sql = " Select Token From users";

	$result = mysqli_query($conn,$sql);
	$tokens = array();

	if(mysqli_num_rows($result) > 0 ){

		while ($row = mysqli_fetch_assoc($result)) {
			$tokens[] = $row["Token"];
		}
	}

	mysqli_close($conn);

	$message = array("message" => "my message");
	$message_status = send_notification($tokens, $message);
	echo $message_status ;



 ?>


how to create this function in c# using wcf service
any suggestion


sorry for my english
Posted
Comments
Afzaal Ahmad Zeeshan 10-Aug-16 8:44am
   
I will recommend using Azure Notification Hubs. They provide a very good ways of managing and sending your push notifications to your devices, not just Android, but the platform supports Windows Phone, iOS etc.

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