2014-10-16 65 views
5

我想使用解析推送通知服务发送自定义数据,但从包中提取时始终返回空值。无法读取解析推送通知包数据

自定义广播接收器:

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.e("Receiver","Invoked"); 
    Bundle bundle = intent.getExtras(); 
    Intent contentIntent = new Intent(context, MainPage.class); 
    String alertMsg = bundle.getString("heading"); //never get this value 
    String urlString = bundle.getString("dataString"); //never get this value 
    contentIntent.putExtra(EXTRA_URL, urlString); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 
      NOTIFY_REQUEST_CODE, contentIntent, PendingIntent.FLAG_ONE_SHOT); 
    showNotification(context, notificationId, alertMsg, pendingIntent); 
} 

舱单申报:

 <receiver 
     android:name=".receiver.NotificationReceiver" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="link_notification"/> 
     </intent-filter> 
    </receiver> 

,我从parse仪表盘发出以下JSON:

{ "dataString": "objectId", "heading": "type", "action": "link_notification" } 

当我记录捆绑数据时,我能够看到标题dataString,但无法访问它。该包总是返回null。

请帮忙! 谢谢。

回答

16

的JSON将是额外的字符串com.parse.Data

@Override 
public void onReceive(Context context, Intent intent) { 
    Bundle extras = intent.getExtras(); 
    String jsonData = extras.getString("com.parse.Data"); 
    JSONObject jsonObject; 
    try { 
     jsonObject = new JSONObject(jsonData); 
     String heading = jsonObject.getString("heading"); 
     String dataString = jsonObject.getString("dataString"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
0

这是因为包数据包含json对象作为字符串可检索与您可以检查解析的密钥。只有这样你才能用gson解析这个字符串来获取Java对象。

+0

什么是在其中与json字符串配对的键的名称? – AabidMulani 2014-10-17 14:05:53

3

好吧,现在我明白了。为了让您的JSON字符串,你需要这样做:

public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    Bundle extra = intent.getExtras(); 
    String json = extra.getString("com.parse.Data"); 
}