2016-07-22 9 views
5

मैं फायरबेस सूचनाओं को लागू करने की कोशिश कर रहा हूं। लेकिन मुझे फायरबेस सूचना से कस्टम डेटा को पुनर्प्राप्त करने के तरीके पर कोई दस्तावेज ढूंढने में परेशानी है।एंड्रॉइड फ़ायरबेस सूचना से कस्टम डेटा कैसे प्राप्त करें?

Firebase console

लेकिन में कस्टम कुंजी पाने के लिए कोड में।

enter image description here

मैं FirebaseMessagingService.onMessageReceived उपयोग कर रहा हूँ संदेश डेटा प्राप्त करने के।

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // TODO(developer): Handle FCM messages here. 
    // If the application is in the foreground handle both data and notification messages here. 
    // Also if you intend on generating your own notifications as a result of a received FCM 
    // message, here is where that should be initiated. See sendNotification method below. 
    Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
} 

उत्तर

20

आप का उपयोग कर अपने कस्टम डेटा की जाँच कर सकते हैं:

for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) { 
    String key = entry.getKey(); 
    String value = entry.getValue(); 
    Log.d(TAG, "key, " + key + " value " + value); 
} 

विशिष्ट कुंजी प्राप्त करने के लिए:

String value = remoteMessage.getData().get("<YOUR_KEY>"); 
+0

मैं firebase उपयोग कर रहा हूँ देशी एंड्रॉयड प्रतिक्रिया में, जहां मैं पाश के लिए यह उल्लेख करना चाहिए। ... MyFirebaseMessagingService.java में? @ चिंतन सोनी – Vijay

5

अपनी कुंजी बनने दें, तो आप निम्न कोड का उपयोग करके आसानी से इस मूल्य को पार्स कर सकते हैं।

JSONObject json = new JSONObject(remoteMessage.getData()); 
    Iterator itr = json.keys(); 
    while (itr.hasNext()) { 
    String key = (String) itr.next(); 
      if (key.equals("A")) { 
       flag = json.getString(key); 
      } 
      Log.d(TAG, "..." + key + " => " + json.getString(key)); 
    } 
संबंधित मुद्दे