Strapi for Push Notification?

Hello Community,

I am working at a fairly large company. We are in the process of launching our first mobile app. We will have hundreds of thousands of daily users.

My company is already using Strapi (Web). I was wondering if Strapi could be a solution for my mobile app notification project.

Thank you
Eric

Could yoy solve your problem?

We integrated Firebase cloud messaging for push notifications and it is awesome.

Hi,
Can you give me a short guide on how did you achieve it?
code snippet would be helpful.
Do I need to register user to firebase as well ?

Greetings @m90khan. Here is a brief starter for using Firebase Cloud messaging with Strapi.

  1. Install the firebase-admin server into your nodejs project. See here
  2. Yes, you will need to have a Firebase Cloud messaging account to set up a Cloud Messaging project. See here
  3. I created a new Strapi service called notifications. with code like this:
'use strict';

const admin = require('firebase-admin');
const serviceAccount = require("./push-your-project-info.json");

/**
 * `notifications` service.
 * Currently we are using the Google Fiebase Cloud Messaging service
 * to broadcast push notifications to iOS and Android clients`
 */

module.exports = {
	  
  initNotifications() {
  	admin.initializeApp({
  		credential: admin.credential.cert(serviceAccount)
  	});
  	strapi.log.info('Notifications initialized...');
  },
   
  // a topic is automatically created when there is at least 1 subscriber(token) assigned to it
  // a topic is automatically deleted when there are no more subscribers assigned to it
  // so to delete a topic, just remove all of the subscribers for the topic  
  async subscribeToTopic(topic, tokens) {
  	try {
  		let response = await admin.messaging().subscribeToTopic(tokens, topic);
  		return prepareTopicResponse(response, tokens, 'subscribeToTopic', topic);
  	} catch(err) {
  		strapi.log.error('Notifications/subscribeToTopic', err.errorInfo);
			//throw new Error(err);
		}
  },
   
  async unsubscribeFromTopic(topic, tokens) {
  	try {
  		let response = await admin.messaging().unsubscribeFromTopic(tokens, topic);
  		return prepareTopicResponse(response, tokens, 'unsubscribeFromTopic', topic);
  	} catch(err) {
  		strapi.log.error('Notifications/unsubscribeFromTopic', err.errorInfo);
			//throw new Error(err);
		}	
  },
  
  async sendMessageToTopic(topic, title, description, data) {
		const message = {
		  notification: {
		    title: title,
		    body: description
		  },			
		  data: data,
		  topic: topic
		};
		
		let respObj = {
			method: 'sendMessageToTopic',
			topic: topic,
			message: message,
			response: ''
		};
  	
  	try {
  		respObj.response = await admin.messaging().send(message);
  		return respObj;
  	} catch(err)  {
  		strapi.log.error('Notifications/sendMessageToTopic', err.errorInfo);
			// throw new Error(err);
		}
  },
  
	async sendMessageToDevices(tokens, title, description, data) {
		const message = {
		  notification: {
		    title: title,
		    body: description
		  },			
		  data: data,
		  tokens: tokens
		};
		
		try {
			const response = await admin.messaging().sendMulticast(message);
			console.log(prepareTopicResponse(response, tokens, 'sendMessageToDevices', ''));
			return prepareTopicResponse(response, tokens, 'sendMessageToDevices', '');
		} catch(err) {
			strapi.log.error('Notifications/sendMessageToDevices', err.errorInfo);
			//throw new Error(err);
		}
	},   
  1. Call init on the service from your Strapi bootstrap function
module.exports = () => {
	// Initialize the Firebase Cloud Messaging Service
	strapi.services.notifications.initNotifications();
}
  1. Call the notifications methods where needed
  	try {
  		let resp1 = await strapi.services.notifications.subscribeToTopic(topic, tokens);
  		let resp2 = await strapi.services.notifications.sendMessageToDevices(tokens, title, description, data);
  	} catch(err) {
  		strapi.log.error('Notifications/subscribeToTopicAndSendMessageToDevices', err);
  	}