【问题标题】:Responding to a push notification using Parse使用 Parse 响应推送通知
【发布时间】:2014-03-31 12:50:04
【问题描述】:

我正在使用 Parse SDK 在我的 Android 应用中接收推送通知。我的后端发送一个 JSON 格式的字符串,其中包含要在通知栏中显示的推送通知的内容。

我想要做的是解析这个 JSON,并根据它的内容,将用户带到应用程序的不同部分(活动)。我能够很好地接收和解析 JSON,但是如何处理用户点击通知栏中的通知时执行的实际操作?

我知道我可以配置 Parse SDK 在我订阅特定频道时打开不同的活动,如下所示:

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

但这并没有真正帮助我,因为在我真正收到推送通知并解析 JSON 之前,我不知道要打开哪个 Activity。所以,我正在寻找的是这样的:

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 来完成?谢谢。

【问题讨论】:

    标签: android push-notification parse-platform


    【解决方案1】:

    这不是一个直接且好的解决方案,但我认为它确实有效(我自己没有尝试过)。您可以使用该广播接收器并根据您的 JSON 数据调用 PushService.setDefaultPushCallback(context, ActivityAorB.class);。这将设置在收到推送消息时调用的默认活动,以便在用户点击通知时打开此活动。

    请务必将此接收器添加到您的清单中。

    More info about broadcast receiver on parse

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多