1

So I'm trying to make a notification system similar to Facebook's system. I have a cron job running in the background and when it feels the need to send out a notification to an user it uses the PHP (REST) SDK to send a push to Firebase. On the dashboard I have an 'on' callback that fetches all incoming notifications for the user and shows a notification for each incoming new notification

var push_ref = firebase_ref.on('child_added', function(push) {
    new PNotify({
        title: 'Notification',
        text: push.val().message,
        ...
    });
});

However, when I reload the page all previously sent notifications pops up simultaneously and fills the screen with notification boxes. I don't wanna delete the sent push notifications because later on I'm gonna make a system to fetch them and put them in a dropdown box (similar to the notification dropdown on Facebook).

My question is;

How can I keep all sent notifications but at the same time disable that Firebase returns all notifications when the client goes online again?

UPDATE: Looked through their documentation again and saw that their retrieving data types are very different. It says under "child_Added" that it "is triggered once for each existing child and then again every time a new child is added to the specified path". This is why it is triggered once I reload the page with all the existing children.

Can you see any other types that might work for me, because I haven't found any.

Magnus
  • 391
  • 1
  • 7
  • 35
  • It's unclear why they would be triggered again--that's not the normal behavior of Firebase for temporarily dropped connections. Are you utilizing goOffline() or recreating the listeners? It seems like we're missing some important context here. – Kato Jan 30 '15 at 18:23
  • I am only using the code presented here and the line to start a new firebase connection (new Firebase()). @Kato – Magnus Jan 30 '15 at 20:21
  • Check my update, it might help you help me. @Kato – Magnus Jan 30 '15 at 20:30
  • 2
    Thanks for clarifying. I suspect that your use case can be greatly simplified and that you've made this far more complex than necessary. But for the answer to the question you've asked, see [here](http://stackoverflow.com/questions/19883736/how-to-discard-initial-data-in-a-firebase-db) and [here](http://stackoverflow.com/questions/18270995/how-to-retreive-only-new-data). – Kato Jan 31 '15 at 17:38

1 Answers1

3

You could have the client update each message on receipt to acknowledge that it was shown (set acknowledged = true or whatever). Then use the acknowledged field to filter the UI treatment to only highlighting unacknowledged messages.

This assumes the messages are user-specific, or your data structure will require a message/user mapping to hold the acknowledgements.

If the user is reading from a public stream of messages, you could save a user-specific lastReadDate or something that filters out the old messages.

James
  • 11,721
  • 2
  • 35
  • 41
  • Thank you, I'll go with this solution in the meantime while I'm doing further research on the issue. – Magnus Jan 29 '15 at 21:39