2014-03-31 72 views
0

我正在使用Parse SDK在我的Android應用中接收推送通知。我的後端發送一個JSON格式的字符串,並在推送通知的內容中顯示在通知欄中。使用Parse響應推送通知

我想要做的就是解析這個JSON,並根據它的內容,把用戶帶到應用程序的不同部分(Activities)。我能夠接收和解析JSON,但是如何處理當用戶點擊通知欄中的通知時執行的實際操作?

我知道我可以配置解析SDK開闢不同的活動時,我訂閱特定信道,這樣的:

PushService.subscribe(this, "testing", ActivityA.class, R.drawable.ic_push); 
    PushService.subscribe(this, "testing2", ActivityB.class, R.drawable.ic_push); 
    // etc 

但是,這並不能真正幫助我,因爲我不會知道打開哪個活動,直到我實際收到推送通知並解析JSON。所以,我正在尋找的東西是這樣的:

public class PushReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     try { 
      JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data")); 

      if(json contains id = 0) { 
       // open ActivityA when the user opens this notification 
      } else if(json contains id = 1) { 
       // open ActivityB when the user opens this notification 
      } 
     } catch (JSONException e) { 
      Log.d("", "JSONException: " + e.getMessage()); 
     } 
    } 
} 

任何想法如何使用Parse可以實現這一點?謝謝。

回答

0

這不是一個直接的,很好的解決方案,但我認爲它做的工作(自己沒有嘗試過)。您可以使用該廣播接收器並根據您的JSON數據調用PushService.setDefaultPushCallback(context, ActivityAorB.class);。這將設置一個默認活動,當接收到推送消息時被調用,以便當用戶點擊通知時該活動將打開。

一定要將此接收器添加到您的清單。

More info about broadcast receiver on parse