解析 - 接收推送通知时如何防止意图自动启动[Android]

时间:2014-06-23 18:04:30

标签: android broadcastreceiver parse-platform

我已成功使用JSONObject实现预先定位,并能够发送和接收通知。我还设置了默认回调PushService.setDefaultPushCallback(this, myActivity.class);.

我有BroadcastReceiveronReceive(),它将访问通过有效负载传递的所有数据。我在onReceive函数中也有一个Intent,它会激活一个活动,并通过putExtra();将一些额外的数据传递给该活动。

我现在的问题是,当我收到通知时,它会自动启动我在onReceive()功能中声明的意图活动。我想要做的是只有当用户点击/点击系统托盘上的推送通知消息时才启动onReceive的意图活动。

我尝试了很多方法来解决这个问题,但我一直在失败。有什么建议?如果需要,我可以提供额外的代码。谢谢。

编辑:当用户点击推送通知时,无论应用程序位于前台还是后台,都应启动意图活动。感谢。

好吧,我决定在这里粘贴我的onReceive():

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

        Iterator itr = json.keys();
        ArrayList arraylist = new ArrayList();

        while (itr.hasNext()) {
            String key = (String) itr.next();

            arraylist.add(json.getString(key));
        }

        Intent intentComment = new Intent(context, comments.class);
        intentComment.putExtra("post_id", arraylist.get(3).toString());
        intentComment.putExtra("btnFlag", arraylist.get(2).toString());
        intentComment.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intentComment);

    } catch (JSONException e) {
        Log.d(TAG, "JSONException: " + e.getMessage());
    }
}

0 个答案:

没有答案