2014-09-29 50 views
0

在我的Android應用程序中,我使用GCM。我收到消息但沒有數據。解析GCM消息中的數據

我這個JSON發送到GCM的服務器:

{ 
    "data": { 
     "data.senderName": "Marco Minerva", 
     "data.gcm.collapse_key": "chiave_collassata", 
     "data.activityId": "42", 
     "data.senderId": "12", 
     "data.message": "Hai ricevuto una notifica", 
     "data.peerProfileImageUrl": null, 
     "data.landingPage": "MainActivity", 
     "data.isCorporate": false 
    }, 
    "collapse_key": "chiave_collassata" 
} 

onHandleIntent(Intent intent)我已經嘗試了所有的這些,沒有工作,所有的變量都爲空:

String message = extras.getString("msg"); 
String data = extras.getString("data"); 
String m = extras.getString("message"); 

String m1 = intent.getStringExtra("msg"); 
String m2 = intent.getStringExtra("data"); 
String m3 = intent.getStringExtra("message"); 

String senderName = intent.getExtras().getString("data.senderName"); 
String dm = intent.getExtras().getString("data.message"); 

String jsonString = extras.getString("com.parse.Data"); 

回答

0

嘗試:

Bundle extras = intent.getExtras(); 
String senderName = bundle.getString("data.senderName"); 

或者如果我讀錯了試試:

Log.d("message", bundle.toString()); 
Bundle extras = intent.getExtras(); 
try 
{ 
    Object data = bundle.get("data"); 
    Log.d("data", data.toString()); 
} 
catch(Exception e) 
{ 
    Log.e("exception", e.getMessage()); 
} 

這應該告訴你結構和檢查你的日誌貓。

0

,可以獲取屬性,例如:

String message = data.getString("message"); 
    String title= data.getString("title"); 
    String subtitle = data.getString("subtitle"); 
    String ticketText = data.getString("ticketText"); 
    String largeIcon= data.getString("largeIcon"); 
    String smallIcon = data.getString("smallIcon");