How to send device to device messages using Fireba

2018-12-31 08:10发布

After searching the docs I could not find any info on how to send device to device messages using FCM without the use of an external server.

For example, if I was creating a chat application I would need to send push notifications to users about unread messages since they won't be online all the time and I can't have a persistent service in the background that would always be connected to the real time database because that would be too resource heavy.

So how would I send a push notification to a user "A" when a certain user "B" sends him/her a chat message? Do I need an external server for this or can it be done with just Firebase servers?

13条回答
宁负流年不负卿
2楼-- · 2018-12-31 08:36

Google Cloud Functions make it now possible send push notifications from device-to-device without an app server.

From the relevant page on Google Cloud Functions:

Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. In such an app, a function triggered by Realtime Database writes to store new followers could create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.

Example:

  1. The function triggers on writes to the Realtime Database path where followers are stored.

  2. The function composes a message to send via FCM.

  3. FCM sends the notification message to the user's device.

Here is a demo project for sending device-to-device push notifications with Firebase and Google Cloud Functions.

查看更多
登录 后发表回答