현재 Parse Cloud Code Background Job을 사용하여 지정된 반경 내에 새 애완 동물이 발견되면 사용자에게 알립니다.배열로 구문 분석 푸시 요청 감소
예를 들어 푸시 활성화가 설정되어 있고 지정된 반경 내에있는 애완 동물 XYZ에 누락 된 애완 동물에 대한 푸시 알림이 전송됩니다.
누락 된 애완 동물 근처에 푸시 알림이 현재 한 번에 하나씩 보내고 있기 때문에 푸시 알림의 API 요청 제한을 초과한다는 사용자가 많은 경우 문제가 생길 수 있습니다.
어떻게 개선 할 수 있습니까? 나는 여기에
(모든 애완 동물의 텍스트가 동일) 아마 통지를 받고해야 모든 Users_Ids와 배열을 사용하여 생각하고 지금까지 내 (작업) 코드입니다 : 확인되는
홈페이지 작업 기능 새로운 애완 동물
는Parse.Cloud.job("pushRadius", function(request, response) {
Parse.Cloud.useMasterKey();
console.log('pushRadius');
var Pet = Parse.Object.extend("Pet");
var petQuery = new Parse.Query(Pet);
petQuery.equalTo("initialPushSent", false);
petQuery.equalTo("status", "missing");
petQuery.equalTo("deleted", false);
petQuery.find().then(function(pets) {
console.log("found " + pets.length + " pets");
var promises = [];
_.each(pets, function(pet) {
promises.push(checkUsersForPet(pet));
Parse.Cloud.useMasterKey();
pet.set("initialPushSent", true);
pet.save();
});
return Parse.Promise.when(promises);
}).then(function(obj) {
console.log('All Pets checked');
}, function(error) {
console.log('whoops, something went wrong ' + error.message);
});
});
가져 오기 주요 기능
function checkUsersForPet(pet){
console.log("check Pet" + pet.id);
var petLocation = pet.get("lastSeenLocation");
var query = new Parse.Query("User");
query.withinKilometers("lastLocation", petLocation, 50);
query.equalTo("pushActivated", true)
query.find().then(function(users) {
console.log("found " + users.length + " users for pet " + pet.id);
var promises = [];
_.each(users, function(user) {
if (petLocation.kilometersTo(user.get("lastLocation")) <= user.get("pushRadius")) {
promises.push(sendUsersPush(user, pet));
}
});
return Parse.Promise.when(promises);
}).then(function(obj) {
console.log('All Users checked');
}, function(error) {
console.log('whoops, something went wrong ' + error.message);
});
}
보내기 사용자 통지에서 발견 된 각 애완 동물을위한 범위 내에서 사용자와 새로운 알림을 추가 누락 된 애완 동물 푸쉬 보내기위한 객체는 1 API 요청
function sendUsersPush(user, pet){
console.log("send user "+ user.id +" push for pet" + pet.id)
Parse.Push.send({
channels: ["user_" + user.id],
data: {
badge: "Increment",
alert: "Neues vermisstes Tier " + pet.get("title") + " im Umkreis"
}
}, {
success: function() {
console.log("Push sent to user" + user.id + "for pet " + pet.id);
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
}
});
var Notification = Parse.Object.extend("Notification");
var notification = new Notification();
notification.set("read", false);
notification.set("user", user);
notification.set("pet", pet);
notification.set("type", "new");
notification.set("petName", pet.get("title"));
notification.save(null, {
success: function(notification) {
},
error: function(notification, error) {
}
});
}