Firebase push notification vs childAdd listener

2019-01-28 13:39发布

问题:

I'm working in a friend request module for mobile app base on Firebase so I'm considering to choose the way that notification is pushed.

Assume that userA request to be friend with userB. There're 2 ideas now:
- userA send request to a simple server then it will call FCM to send notification to userB.
- Make a service that listen to data changed in Firebase realtime database then userA will make change on that db and notification will be shown on userB device.

I think both are possible to implement but what is better, and why? Please give me some advice about this..

Thanks in advance.

回答1:

Using either one should be fine.

However, a point to consider here is when keeping a listener active for the Real-time database, it also keeps an open socket on the user's device which adds to battery consumption.

While for FCM, it will only trigger once there is a notification is needed to be sent. If a friend request isn't really that app critical, I think using FCM is a way to go.

Have you also considered using both? If the user is currently online, it would be good to use the Real-time DB, but the childAdded won't be triggered if the user is offline (not using the app for instance). In that case, you can set it so that a notification will be sent to the user.

The important thing in your scenario is that the friend request should be saved first in your database or app server, so that it will trigger the corresponding action (FCM notification or Real-time DB update).