Firebase Cloud Messaging via Laravel

Tram Ho

I. Introduction

  • As the title, this article I’ll show how to send notification using Firebase and Laravel, can subscribe topic is multiple device token qua which enhance the ability to send notification, (a problem I’ve encountered in the project last) , using queue …
  • There will be many articles on how to subscribe topic by device token, how to subscribe topic when entering an action, but in this article, I will guide each case accordingly when you want to push at a certain time with the number of devices. Certain tokens follow any rule.

II. Instructions on how to

1. How to get server key

  • There are many articles on how to create an app on firebase and you can follow and get the key in the Cloud Messaging (Project Setting) tab as Server key:

2. Setup in Laravel

  • Create a NotificationService interface

  • Create An FcmService as follows

  • The meaning of each function

subscribeTopic : This function works to subscribe topics on firebase and works with multiple device tokens.

unsubscribeTopic Topic: This function works to unsubscribe topic (cancel topic) on firebase and works with many device tokens.

sendNotification : Based on the topic name and the device token that have been registered, this function sends notifications based on the topic submitted via the subscribeTopic function, the meaning of attrubte.

+ to: to know push notification to topic nafo

+ notification: the title and content of the notification

+ apns (for IOS): predefined options such as image

+ data: add custom attributes

sendBatchNotification : This function will perform the serialized sendBatchNotification -> sendNotification -> unsubscribeTopic .

  • Create PushNotificationJob file as follows:

  • Now let’s see how it works !!! Let’s say there is a problem: Gửi notification đến những device có ngày sinh nhật vào ngày 25-10 (With device tokens stored in the User table)

III. End

  • Above is how to send push notifications set up on the server using laravel. Hope to be of help to you in the product development process m /
Share the news now

Source : Viblo