2016-07-08 61 views
3

我在我的Android和iOS應用程序中使用FCM。客戶端代碼工作正常,因爲通過Firebase控制檯,我可以向兩個平臺發送沒有任何問題的通知。通過我的C#代碼,我可以成功地向Android設備發送通知,但通知不會出現在iPhone上,除非直接來自Firebase通知控制檯。我不知道是什麼給了。Firebase雲消息傳遞和C#服務器端代碼

C#服務器端代碼

try 
{ 
    var applicationID = "application_id"; 
    var senderId = "sender_id"; 
    string deviceId = "device_id_of_reciever"; 
    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 = "This is the message", 
      title = "This is the title", 
      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(); 
       Response.Write(sResponseFromServer); 
      } 
     } 
    } 
} 
catch (Exception ex) 
{ 
    Response.Write(ex.Message); 
} 

通知不工作在iPhone上我的服務器端代碼,但我從火力地堡了良好的反響。

{ 
    "multicast_id": 479608 XXXXXX529964, 
    "success": 1, 
    "failure": 0, 
    "canonical_ids": 0, 
    "results": [{ 
     "message_id": "0:1467935842135743%a13567c6a13567c6" 
    }] 
} 

任何幫助或建議將非常感激。

+0

這些消息直接來自Firebase時看起來有所不同。 gcm.message_id:0:1467938951987907%a13567c6XXXX67c6] %@ [aps:{alert = { body =「這是消息」; title =「這是標題」; }; },gcm.message_id:0:14679XXXX87907%a13567c6a1XXXXc6] 消息收到 消息ID:0:1467939XXXXX5%a13567c6a13567c6 %@ [google.c.a.c_l:why2,google.c.a.e:1,APS:{ 警報= why1; },gcm.ne:1,google.cac_id:50607XXXXXX957125,google.caudt:0,gcm.message_id:0:14679XXXX18215%a13567c6a1XXX7c6,google.cats:146XXXXX32] –

+0

嗨,你能告訴我如何發送deviceId ? –

回答

4

嘗試在您的FCM請求中將優先級字段設置爲高。

如:

var data = new 
{ 
    to = deviceId, 
    notification = new 
    { 
     body = "This is the message", 
     title = "This is the title", 
     icon = "myicon" 
    }, 
    priority = "high" 
}; 

注意,雖然在發展,使用高優先級是好的,但在生產中,當用戶預計將採取行動,就像回覆聊天消息,它應該只被使用。

+0

我標記答案正確。就我而言,我必須將優先級設置爲10(優先級= 10),然後一切正常。提供的答案是非常有用的。謝謝亞瑟 –

相關問題