2016-07-04 24 views

risposta

10

C# Server Side Codice Per Firebase cloud Messaging

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Web; 
using System.Web.Script.Serialization; 

namespace Sch_WCFApplication 
{ 
    public class PushNotification 
    { 
     public PushNotification(Plobj obj) 
     { 
      try 
      {  
       var applicationID = "AIza---------4GcVJj4dI"; 

       var senderId = "57-------55"; 

       string deviceId = "euxqdp------ioIdL87abVL"; 

       WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); 

       tRequest.Method = "post"; 

       tRequest.ContentType = "application/json"; 

       var data = new 

       { 

        to = deviceId, 

        notification = new 

        { 

         body = obj.Message, 

         title = obj.TagMsg, 

         icon = "myicon" 

        }  
       };  

       var serializer = new JavaScriptSerializer(); 

       var json = serializer.Serialize(data); 

       Byte[] byteArray = Encoding.UTF8.GetBytes(json); 

       tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID)); 

       tRequest.Headers.Add(string.Format("Sender: id={0}", senderId)); 

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

           string str = sResponseFromServer; 

          }  
         }  
        }  
       }  
      }   

      catch (Exception ex) 
      { 

       string str = ex.Message; 

      }   

     } 

    } 
} 

apikey e SenderID, si ottiene è qui --------- come segue (sotto le immagini) (vai al il vostro Firebase App)

Step. 1

Step. 2

Step. 3

+3

dove ottenere il dispositivo id da? –

+0

Si tratta di un token generato utilizzando un metodo FirebaseInstanceId.getInstance(). GetToken() in un servizio –

+0

Lo abbiamo capito .. il servizio è FirebaseInstanceIdService. Inoltre, tRequest.GetResponse() genera "Il server remoto ha restituito un errore: (401) Non autorizzato". Errore, anche dopo aver fatto cosa ha detto qui-> http://stackoverflow.com/questions/10205854/error-the-remote-server-returned-an-error-401-unauthorized Altre soluzioni? –

0

Non credo c'è qualche cambiamento nel modo in cui si inviano notifiche push. In FCM anche, che si sta per fare HTTP POST Richiesta allo stesso modo che hai fatto per GCM:

https://fcm.googleapis.com/fcm/send 
Content-Type:application/json 
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA 

{ "data": { 
    "score": "5x1", 
    "time": "15:10" 
    }, 
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..." 
} 

leggere su FCM Server per ulteriori informazioni.

L'unico cambiamento che ho potuto vedere ora è l'Url di destinazione. Periodo.

1

Ecco il mio esempio VBScript per chi preferisce vb:

//Create Json body 
posturl="https://fcm.googleapis.com/fcm/send" 
body=body & "{ ""notification"": {" 
body=body & """title"": ""Your Title""," 
body=body & """text"": ""Your Text""," 
body=body & "}," 
body=body & """to"" : ""target Token""}" 

//Set Headers :Content Type and server key 
set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP") 
xmlhttp.Open "POST",posturl,false 
xmlhttp.setRequestHeader "Content-Type", "application/json" 
xmlhttp.setRequestHeader "Authorization", "Your Server key" 

xmlhttp.send body 
result= xmlhttp.responseText 
//response.write result to check Firebase response 
Set xmlhttp = nothing