Push notification on meteor

Hi!
I want to push notification to all client, when added data to collection and count new document added.
Screenshot%20from%202019-08-27%2018-41-13

How can I do, please help me ?

1 Like

Push notifications are notoriously complicated to implement on your own - the data flows are very complicated.

You’re better served to turn to a provider who has an easy-to-use API. I’ve had good experiences with OneSignal who also have a generous free tier.

I had this problem a little while ago: https://forums.meteor.com/t/fire-event-without-user-interaction/49523/3

I did it with a Interval that checks every second if a notification that gets in is set on false on the isSeen field.

Template.nav.onRendered(function (template) {
    Meteor.setTimeout(function(){ 
        const pnCount = PN.find({userGet: Meteor.user().username, isSeen: false, isTrash: false, isFav: false}, {sort: {createdAt: -1}}).count();
        Session.set('pnCount', pnCount);
      }, 1000);

      Meteor.setInterval(function(){

        var pnCount = Session.get('pnCount');
        const pnCountNew = PN.find({userGet: Meteor.user().username, isSeen: false, isTrash: false, isFav: false}, {sort: {createdAt: -1}}).count();
      
        if (pnCountNew > pnCount) {
          var audio = new Audio('sounds/blep.wav');
          audio.play();
          Session.set('pnCount', pnCountNew);
          console.log("Testlog: There is a new pn");
      
        } else {
          console.log("Testlog: There is none");
        }
      }, 3000);
  });

However, sometimes the browser won’t allow the sound-file to be played.
Coagmano suggested this approach, where you check your collection for changes

PN.find({
        userGet: Meteor.user().username,
        isSeen: false,
        isTrash: false,
        isFav: false,
    },
    { sort: { createdAt: -1 } }
).observeChanges({
    added() {
        var audio = new Audio('sounds/blep.wav');
        audio.play();
        console.log('Testlog: There is a new pn');
    },
});

which is basically less complicated than my approach but works the same.

OneSignal cannot ensure privacy. If your type of business doesn’t require privacy they might be ok …

1 Like

What is your exact case/problem here. The Push technology or the “notification to all client, when added data to collection and count new document added.”

I want to show client when I added new document.

Do you have any code you already wrote?

Here my code :


Meteor.publish('Notifications', function(selector) {   
  
  var self = this;
  let count = 0;
  
   const handler = Notification.find(selector).observeChanges({
     added: (id) => {
       count += 1;
     }
  })
   self.ready()

   console.log('1',count);
  self.onStop(() => handler.stop())

   return count
})

But when I reload page , it get data again. I want clear when clients click notification.

I’m not sure but the reason could be that the code is on the server? Maybe try relocate the observeChanges to the client instead?

Thank for your reply, I will try it on clients.