将推送通知发送给0个收件人

时间:2014-07-20 18:58:40

标签: javascript ios parse-platform

我正在尝试让我的应用向个别用户发送推送通知(用户之前已注册过)。我尝试做的目的是告知用户他们之前在我的商店发出的订单已经发货。

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *loggedUser = [userDefaults stringForKey:@"loggedUser"];

PFInstallation *installation = [PFInstallation currentInstallation];
[installation addUniqueObject:loggedUser forKey:@"channels"];
[installation saveInBackground];

(我没有使用PFUser,而是变量loggedUser可以完美地运行。)

上面的代码似乎运行正常,因为如果我在Parse上访问数据浏览器,我可以在安装表,频道列上看到用户名。

但是,虽然我在云代码上看到了用户,这意味着我在发送推送通知时已成功上传,但我发现它已发送给0个收件人。

推送是通过JavaScript发送的,使用以下代码:

(变量user如上所述,我确保它也能正常工作。)

Parse.Push.send({
    channels: "[\""+user+"\"]",
    data: {
        alert: "Your order has been shipped."
    }
}, {
    success: function() {
        alert('The push notification has been sent correctly')
    },
    error: function(error) {
        alert('Error: '+ error)
    }
});

有人知道我做错了什么吗?是channels:写得不正确吗?或者是因为我无法在此Parse发送操作中使用变量?


修改

这是整个文件

<script type="text/javascript">
Parse.$ = jQuery;
Parse.initialize("APPLICATION ID", "JAVASCRIPT KEY");

var PhotoObject = Parse.Object.extend("PhotoObject");
var query = new Parse.Query(PhotoObject);
query.equalTo("allowed", false);
query.equalTo("appear", true);
query.descending("createdAt");
query.find({

    success: function(results) {

        // Do something with the returned Parse.Object values
        for (var i = 0; i < results.length; i++) { 
        var object = results[i];
        var profilePhoto = object.get("image");
        var description = object.get("description");
        var location = object.get("location");
        var user = object.get("user");
        var copyLocation = '';
        var copyDescription = '';
        var copyUser = '';

        if (description === '') {
            copyDescription = '<em>No description.</em>';
        }else {
            copyDescription = description;
        }

        if (location === '') {
            copyDescription = '<em>Could not determine location.</em>';
        }else {
            copyLocation = location;
        }

        if (user === undefined ) {
            copyUser = '<em>Could not determine user.</em>';
        }else {
            copyUser = user;
        }

        $('#images').append("<li class='listItem"+i+"' id='"+object.id+"listItem''><img class='liPhoto' id='imagesID"+i+"' src='"+profilePhoto.url()+"'/></li>");
        $("#imagesID"+i).wrap("<a href='"+profilePhoto.url()+"'/>");

        $(".listItem"+i).append("<p class='description'><strong>Description: </strong>"+copyDescription+"<br><br><strong>User: </strong>"+copyUser+"<br><br><strong>Location: </strong>"+copyLocation+"</p>");

        $(".listItem"+i).append("<br><br><button class='selectionButton' id='"+object.id+"'>Accept</button><button class='selectionButton'id='"+object.id+"decline'>Decline</button>");

        }

        $("button").click(function() {

            if ((this.id).indexOf("decline") <= 0){

                console.log('approve button pressed');
                var query = new Parse.Query(PhotoObject);
                query.get(this.id, {


                    success: function(photoObject) {

                        // The object was retrieved successfully.
                        photoObject.set("allowed", true);

                        photoObject.save(null, {

                            success: function(photoObject) {

                                // Execute any logic that should take place after the object is saved.
                                var idName = '#'+photoObject.id+'listItem';
                                console.log(idName);
                                $(idName).slideUp();

                                alert('The image was approved succesfully - '+ photoObject.id);

                                Parse.Push.send({
                                    channels: [user],

                                    data: {
                                        alert: "Your image has been successfully moderated and has been published on the gallery!"
                                    }
                                }, {
                                    success: function() {
                                        alert('The push notification has been sent correctly')
                                    },

                                    error: function(error) {
                                        console.log(error);
                                        alert('Error: '+ error)
                                    }
                                });
                            },


                            error: function(photoObject, error) {
                                // Execute any logic that should take place if the save fails.
                                // error is a Parse.Error with an error code and description.
                                        alert('Failed to approve the image, with error code: ' + error.description);
                            }

                        });

                    },

                    error: function(object, error) {
                        // The object was not retrieved successfully.
                        // error is a Parse.Error with an error code and description.
                    }

                });

            } else {
                console.log('decline button pressed');
                var query = new Parse.Query(PhotoObject);
                var idFormated = this.id.replace('decline','');
                console.log(idFormated);

                query.get(idFormated, {

                    success: function(photoObject) {
                        // The object was retrieved successfully.
                        photoObject.set("appear", false);

                        photoObject.save(null, {

                            success: function(photoObject) {
                                // Execute any logic that should take place after the object is saved.
                                var idName = '#'+photoObject.id+'listItem';
                                console.log(idName);
                                $(idName).slideUp();

                                alert('The image was declined succesfully - '+ photoObject.id);
                            },

                            error: function(photoObject, error) {
                                // Execute any logic that should take place if the save fails.
                                // error is a Parse.Error with an error code and description.
                                alert('Failed to declined the image, with error code: ' + error.description);
                            }
                        });

                    },

                    error: function(object, error) {
                        // The object was not retrieved successfully.
                        alert('Failed to declined the image, with error code: ' + error.description);
                    }
                });

            }
        });
    },

    error: function(error) {
        alert("Error: " + error.code + " " + error.message);
    }
    });
</script>

2 个答案:

答案 0 :(得分:0)

我要做的第一件事就是改变你在Parse.Push.send()

中发送频道的方式
channels: [user],

假设user是一个与前一代码匹配loggedUser的字符串。

答案 1 :(得分:0)

我没有指定我想要更改的对象。 而不是:

object.get('user');

是:

photoObject.get('user');