Share about using Firebase Cloud Message (FCM)

Tram Ho

I. Introduction

As the title, this article I would like to share about the use of FCM – through my casting when there are relevant issues for you who are not and about to use FCM can use without being acquired.

II. content

  • FCM is already famous, the old version is legacy http and the latest version is currently http v1.
  • About the difference, you can see here
  • Another problem that the above link doesn’t mention is that when using direct send, http v1 sends 500 devices / batch and legacy http gets 1000 devices / batch.
  • So, there are ways to send in FCM? According to my research, there are 2 ways that are sent by group and by topic . Let’s learn about these 2 ways (In this article, I will guide you by using legacy http , and using http v1 you can refer to the Firebase Admin SDK for PHP package)
  • First, to make it easier to understand, I have available the excute function to manipulate FCM

1. Send via group

  • Is to send by a set of device tokens that are predefined (possibly by querying from any problem) The example has the following problem: Send notificaiton to all devices

Looking at this, it can be seen that if the query data is increasing day by day, is it good to send this directly? Definitely not good. So we have the second way is to send via topic

2. Send via topic

  • In order to do this, I will take a look at the 2 subscribe topic and unsubcribe topic of FCM

With the above problem, the most logical way is that every time the user registers an account, you must subscribe to the topic at that time ( all_devices topic is all_devices ).

  • Subscribe topic

So when you need to send notification to (Maybe tremor in cronjob example) you just replace registration_ids into to the topic: such

  • And the unsubscribe topic is only done when there is an event like removing the app, or updating the device token …

  • Note: The problem with this send method is if the method of sending send via group with cycle subscribe -> send -> unsubscribe because FCM works async , which means that when you do this cycle simultaneously, for example for 2000 devices then you will have to chunk devices into blocks of 1000 and resort to foreach , something like this

Looking at this, you can see that the loop will run 2 times corresponding to 2 batches, and these 2 batches can completely have the time to run concurrently with each other then there will be cases such as: 1 devices receive notifications multiple times or No device will not receive it once. Please note, hàm send chỉ được phép gọi 1 lần nếu sử dụng subscribe topic

  • Another similar problem for using the subscribe topic is: Get notifications from the models (Player, Streamer, influencer … or any shop or salon) you’re following so you can subscribe to a topic. As soon as the user follows the action follow.
  • For all posts that are not as fixed as the follow model or send all as above, you should use send via group for example:
  • Send notifications to users who register for an assignment on any given day.
  • Send notifications to users with any birthdays …. etc.

III. summary

Hope this post can help you master the FCM and when to use send via group and when to use send via topic

Share the news now

Source : Viblo