Push Notifications on Database Updated

Hi, I ask for other help: P

I’ve integrated Push Notifications in my App, how can I send Push Notifications to single users when a database value is changed/updated?

I show you the code for entering the data:

Router.route("myZizool", function() {
	this.render("myZizool", {
		data: function() {
			var dealers = Dealers.find();
			var cnt = dealers.count();
			var cntStolen = Dealers.find({ stolen: true }).count();
			var user = Meteor.user();
			return {
				dealers: dealers,
				count: cnt == 1 ? "1 esercente" : cnt + " esercenti",
				stolenDealers: cntStolen == 1 ? "c'è un esercente segnalato" : (cntStolen == 0 ? "non ci sono Esercenti segnalati" : "ci sono " + cntStolen + " Esercenti segnalati"),
				profile: user.profile,
				email: user.emails[0].address,
				username: user.username
			}
		}
	});
}, {
	waitOn: function() {
		return [
			Meteor.subscribe("myDealers"),
			Meteor.subscribe('images')
		]
	},
	path: "/myZizool"
});
Router.route("editProfile", function() {
	this.render("editProfile", {
		data: function() {
			var user = Meteor.user();
			return {
				profile: user.profile,
				email: user.emails[0].address,
				username: user.username
			}
		}
	})
}, {
	path: "/editProfile"
});
Template.editProfile.events({
	'click span': function(e) {
		var target = $("#"+$(e.currentTarget).data("shows"));
		if(typeof target.attr("disabled") == "undefined" || !target.attr("disabled")) {
			target.fadeIn(1000);
			$(e.currentTarget).fadeOut(1000);
		}
	},
	'change input': function(e) {
		var currentInput = $(e.currentTarget);
		if(currentInput.val() != "") {
			currentInput.fadeOut(1000);
			$("#"+currentInput.attr("id").replace("form-", "")).fadeIn();
			Meteor.call("changeProfileField", currentInput.attr("name"), currentInput.val());
		}
	}
});
Router.route("newDealer", function() {
	this.render("newDealer");
}, {
	waitOn: function() {
		return [
			Meteor.subscribe("images")
		]
	},
	path: "/newDealer"
});

Router.route("editDealer", function() {
	this.render("editDealer", {
		data: function() {
			return Dealers.findOne(this.params._id)
		}
	});
}, {
	waitOn: function() {
		return [
			Meteor.subscribe("myDealers"),
			Meteor.subscribe("images")
		]
	},
	path: "/editDealer/:_id"
});

Template.dealer.events({
	'click #setAsStolen': function() {
		Dealers.update(this._id, {
			$set: {
				stolen: !this.stolen
			}
		});
	},
	'click #delete': function() {
		if(confirm("Sei sicuro di voler rimuovere quest'Azienda? ATTENZIONE: Questa azione non può essere annullata"))
			Dealers.remove(this._id);
	}
});

Template.dealer.helpers({
	isStolen: function() {
		return this.stolen == true;
	},
	payment: function() {
		return payments[this.payment];
	}
});

Template.newDealer.events({
	"submit #insertDealerForm": function() {
		if(AutoForm.validateForm("insertDealerForm")) {
			alert("Azienda inserita con successo!");
			Router.go(Router.path("myZizool"));
		}
	}
});

This is the code for Push Notifications Client:

Push.Configure({
  android: {
    senderID: 372307788345,
    alert: true,
    badge: true,
    sound: true,
    vibrate: true,
    clearNotifications: true
    // icon: '',
    // iconColor: ''
  },
  ios: {
    alert: true,
    badge: true,
    sound: true
  }
});


Push.send({
        from: 'push',
        title: 'Push Notification',
        text: 'Push Notification text',
        badge: 1, //optional, use it to set badge count of the receiver when the app is in background.
        query: {} // Query the appCollection
        // token: appId or token eg. "{ apn: token }"
        // tokens: array of appId's or tokens
        // payload: user data
        // delayUntil: Date
});

This is the code for Push Notifications Server:

Push.Configure({
        gcm: {
        projectNumber: 372307788345,
        apiKey: 'AAAAVq9GAjk:APA91bHFYE4fcR0rZao6kfmX1c6eQneUeH_Oobiznc-YHoQo6QqurBP8Kh0mssT48YWhcojXRqXbzg-dREz6jTax-9fPpEhvxTq7uPxCrRGuiI0Ec4RQ2sWIW4xjVQN1O6GyRO-3RUKR'
        }
    });

You can use Collection.find().observe() or .observeChanges() to set up a functino that runs whenever something changes :slight_smile:

Another option is https://atmospherejs.com/matb33/collection-hooks

Essentially it overrides the default .insert(), .upsert(), .update() and .remove() functions so you can run some code before or after the operation has been completed.

Thank you :smiley: now I do the tests.