Send android push notification when new doc arrives at sync gateway?


#1

Hi i am developing an android chat app using CBL now i need to send push notification(using Google Cloud Messaging) to user when new chat message(doc) arrives at couchbase db through sync gateway how i can do this ?


#2

Hi @psrinivasgoud,

You can get Replicator’s change event through Replicator ChangeListener. http://developer.couchbase.com/mobile/develop/references/couchbase-lite/couchbase-lite/replication/replication/index.html#void-changedelegatechangeevent-event

If you’d like to know which document is updated, please use Database ChangeListener.
http://developer.couchbase.com/mobile/develop/references/couchbase-lite/couchbase-lite/database/database/index.html#void-changedelegatechangeevent-event

I hope this helps you.

Thanks!


#3

HI @hideki thanks for reply . this for client side right i added addChangeListener to database but its working only when app is running when i closed app its not working (even tried with android service) that why i need server side listener.


#5

Hello @psrinivasgoud,

There are two options for making sure client applications have the latest data from sync gateway (in this case, chat messages):

  • use a continuous pull replication
  • use push notifications from the server as you mentioned. Check out the Changes Worker Pattern guide http://developer.couchbase.com/mobile/develop/guides/sync-gateway/changes-worker/index.html. You can use https://github.com/iriscouch/follow (or the equivalent module in the back-end language of your choice) to listen to the changes feed of Sync Gateway (it’s the same api as CouchDB so any changes feed listener module for CouchDB will work with Sync Gateway too). Whenever a message type document is received by Sync Gateway, you can catch this change in the changes feed and then get the user profile documents which may contain the device token(s) to send the push notifications to (there are lots of modules for sending notifications as well. For example https://github.com/ToothlessGear/node-gcm).

Thanks
James


#6

Hi @psrinivasgoud,

You can also check out this blog post on using push notifications with Couchbase Mobile http://blog.couchbase.com/tutorial-implement-android-push-notifications-in-your-couchbase-mobile-app

In this case, the wakeful service executes the pull replication regardless of whether the app is opened or closed.

James


#7

Thanks a lot @jamiltz


#8

Hi team . How sync_gateway can be configured to send new data to firebase cloud message fcm. I want every time sync gateway get new object it sends it to fcm and fcm handle the push notification to the device with couchbase lite.