2017-04-04 75 views
0

我有一個Android應用程序,它調用Web服務來向我們發送消息以獲得支持。通過SDK發送時,無法讀取Firebase通知中的自定義數據

有一個網站,我們開發的地方可以回覆消息,我們使用Firebase。到現在爲止,我們只是發送像這樣一個標題和一個信息:

var applicationID = "snip"; 
var senderId = "snip"; 
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); 
tRequest.Method = "post"; 
tRequest.ContentType = "application/json"; 

var data = new 
{ 
    to = model.RequestFirebaseID, 
    notification = new 
    { 
     body = model.ResponseMessage, 
     title = model.ResponseTitle 
    } 
}; 

在Android應用我能夠使用Java代碼

String notificationTitle = remoteMessage.getNotification().getTitle(); 
String notificationBody = remoteMessage.getNotification().getBody(); 

不過現在我來提取郵件的標題試圖發送回叫ID的自定義字段,所以我修改代碼以

var data = new 
{ 
    to = model.RequestFirebaseID, 
    notification = new 
    { 
     body = model.ResponseMessage, 
     title = model.ResponseTitle, 
     requestid = model.id 
    } 
}; 

所以現在當我使用火力地堡我有這樣的代碼,試圖讀取ID FIEL消息發送回設備d。

String messageID = remoteMessage.getData().get("requestid"); 

但是,這會導致結果爲空。

因此,我嘗試過通過Firebase控制檯發送此測試,我向自定義數據部分添加了requestid併爲其提供了一個值,上面的代碼能夠讀取它。

看來,當我通過Web應用程序發送它無法看到requestid字段。

回答

1

您必須在有效負載內包含data消息。類似這樣的:

var payload = new 
{ 
    to = model.RequestFirebaseID, 
    notification = new 
    { 
     body = model.ResponseMessage, 
     title = model.ResponseTitle 
    }, 
    data = new 
    { 
     requestid = model.id 
    } 
}; 

我將根變量名更改爲​​以區分它。

如果您在使用Firebase通知控制檯發送消息時添加自定義數據,則它將包含在data消息參數中,而不是notification消息中。請參閱Message Types文檔瞭解這兩者的區別。

+0

是的,我想出來後,謝謝 – andrewb