如何使用firebase从WCF服务向android发送通知 [英] How to send notification from WCF service to android using firebase

查看:55
本文介绍了如何使用firebase从WCF服务向android发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个wcf服务来向我的Android应用程序发送通知但是它无法正常工作



我尝试了什么:



我的wcf方法是





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


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;



        }





我的iService:





my iService:

[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);





如果我的Android应用程序启动我使用服务来保存设备令牌



我使用PHP发送成功完整通知但我需要使用wcf服务

php代码是





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 

	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 ;



 ?>





如何使用wcf服务在c#中创建此功能

任何建议





抱歉我的英语



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


sorry for my english

推荐答案

令牌,


消息)
{


message) {


url = ' https://fcm.googleapis.com/fcm/send';
url = 'https://fcm.googleapis.com/fcm/send';


这篇关于如何使用firebase从WCF服务向android发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆