Parse.com ParseQuery不稳定

时间:2014-07-16 17:06:45

标签: android parse-platform

我有一个非常奇怪的情况。我有一个应用程序,我正在实现一个"通知历史"。

我有一个单独的应用程序,它将推送通知发送到目标通道,然后在名为Notifications的表中创建一个条目,保存目标通道和发送的消息。

channels = channelEditText.getText().toString();
                message = messageEditText.getText().toString();
                ParsePush push = new ParsePush();
                push.setChannel(channels);
                push.setMessage(message);
                push.sendInBackground();
                channelEditText.setText("");
                messageEditText.setText("");

                ParseObject notifications = new ParseObject("Notifications");
                notifications.add("channels", channels);
                notifications.put("msg", message);
                notifications.saveInBackground();

我的Android应用"通知历史"片段然后执行

ParseQueryAdapter<ParseObject> notificationAdapter =
            new ParseQueryAdapter<ParseObject>(getActivity(), new ParseQueryAdapter.QueryFactory<ParseObject>() {
                public ParseQuery<ParseObject> create() {
                    ParseQuery query = new ParseQuery("Notifications");
                    query.whereContainedIn("channels", ParseInstallation.getCurrentInstallation().getList("channels"));
                    query.orderByDescending("createdAt");
                    return query;
                }
            });
        notificationAdapter.setTextKey("msg");
        ListView notificationListView = (ListView) rootView.findViewById(R.id.notificationListView);
        notificationListView.setAdapter(notificationAdapter);

我的ParseApplication.java订阅用户到频道:&#34;欢迎&#34;在安装时,我没有收到空指针。通知表条目带有频道&#34;欢迎&#34;填充列表视图。

我有两种方式订阅频道。一种方法就是设备本身就像这样

final EditText syncInput = (EditText) rootView.findViewById(R.id.syncInput);
        Button syncButton = (Button) rootView.findViewById(R.id.syncButton);

        syncButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v){

                String sync = null;
                sync = syncInput.getText().toString();
                PushService.subscribe(getActivity(), sync, DashboardActivity.class);
                syncInput.setText("");
            }

        });

另一种方式是通过CloudCode

Parse.Cloud.define("subscribeToChannel", function(request, response){
var channelName = request.params.channel;
var userId = request.params.userId;

if(!channelName) {
    response.error("Missing parameter: channel");
    return;
}

if (!userId) {
    response.error("Missing paremeter: userId");
    return;
}

//Create a Pointer to the user based on their object id
var user = new Parse.User();
user.id = userId;

Parse.Cloud.useMasterKey();

// A user might have more than one installation
var query = new Parse.Query(Parse.Installation);
query.equalTo("user", user); //Match Installations with a pointer to this User
query.find({
    success: function(installations) {
        for (var i = 0; i < installations.length; i++) {
            //Add the channel to al the installations for this user
            installations[i].addUnique("channels", channelName);
        }

        //Save all the installations
        Parse.Object.saveAll(installations, {
            success: function(installations) {
                //All the installations where saved.
                response.success("All the installations were updated with this channel.");
            },
            error: function(error) {
                //An error occured while saving one of the objects.
                console.error(error);
                response.error("An error occured while updating this user's installations.");
            }
        });
    },
    error: function(error) {
        console.error(error);
        response.error("An error occurred while looking up this user's installations");
    }
});

});

两种订阅方式都是成功的,因为发送到目标通道的推送通知到达设备。这是问题...如果我使用设备订阅我的查询将显示发送到通知表中保存的消息。如果我使用CloudCode,我的查询不会显示发送到通知表中保存的频道的消息。

我很难过。非常感谢任何帮助。

-------------------------------- SOLUTION -------------- -----------------------------------

protected void onResume() {
     super.onResume();
    ParseInstallation.getCurrentInstallation().refreshInBackground(new RefreshCallback(){

        @Override
        public void done(ParseObject parseObject, ParseException e) {
             List<String> channels = ParseInstallation.getCurrentInstallation().getList("channels");
                for (int i = 0; i < channels.size(); i++) {
                    Log.w("TEST", channels.get(i));

        }
        }
    });
}

1 个答案:

答案 0 :(得分:0)

您在Cloud Code中编辑服务器端的安装记录,但设备未获取更新的数据。如果这是您应用中的常见行为,请在加载应用时刷新安装对象:

ParseInstallation.getCurrentInstallation().refreshInBackground();

或fetchInBackground,如下所示:https://parse.com/docs/android_guide#objects-retrieving

这也可以通过调用执行查询的云功能而不是从设备查询来解决(已经在服务器端使用更新的频道列表。)