Installation
From NPM:
npm install strapi-plugin-fcm
From YARN:
yarn add strapi-plugin-fcm
From Git:
- Clone the plugin into your Strapi project
cd /<path-to-your-strapi-project>/src
cd plugins
git clone https://github.com/itisnajim/strapi-plugin-fcm.git strapi-plugin-fcm
cd strapi-plugin-fcm && yarn install
- Enable the plugin in
<root>/config/plugins.js
.
module.exports = {
'strapi-plugin-fcm': {
enabled: true,
resolve: './src/plugins/strapi-plugin-fcm'
},
}
- Build the plugin
yarn build
yarn develop
Configuration
- In the Firebase console, open Settings > Service Accounts.
- Click Generate New Private Key, then confirm by clicking Generate Key.
- Past the content of your downloaded service account json file into FCM Plugin Configuration > serviceAccount. (like in the picture below, then you may need to restart the server)
-
In the same interface 'FCM Plugin Configuration', optionally you can provide where the devices tokens are stored, in the picture example above, I store them in User -> deviceToken (strapi generate the users database table with the name up_users).
-
Optionally you can provide all the topics you have, in the 'FCM Topic' collection type (via the dashboard or via the api - Post requests).
Usage
Via the dashboard
- Enter the notification content.
- Select targets to send to.
- Click send!
Another way via the dashboard.
- you can create a new entry in the 'FCM Notification' collection type and click publish to send to the FCM.
Via the api
- First you have to enable and give routes permissions to a specific role or roles.
- Then via an Http Client (axios, ajax, postman, curl or whatever) send a post request with the body data:
{
"data": {
"title": "OKey",
"body": "Test body",
"image": "",
"payload": "",
"targetType": "topics",
"target": "client_android",
}
}
- You can send an array too:
{
"data": [{...entry1}, {...entry2}, {...entry3}, ...]
}
Trick
If you have saved the entries in the FCM Notification collection as drafts, you can scheduled them to be sent to FCM at a later time.
References