在GCM消息中解析数据

时间:2014-09-29 15:29:08

标签: android google-cloud-messaging

在我的Android应用程序中,我使用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)我尝试了所有这些并且都没有用,所有变量都是NULL:

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

2 个答案:

答案 0 :(得分: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() );
}

这应该显示结构并检查你的日志猫。

答案 1 :(得分: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");