Firebase Cloud Messaging with Flutter - easy steps
Hi, today I'm writing this article because of some difficulties which I have faced in integrating cloud functions into my Flutter mobile app. Here what I'm going to do is trigger a notification from firebase cloud function and push notification in the flutter app.
I hope you have done the basic setup in the flutter app as firebase_messaging documentation says as below. No need to repeat the same thing so I'm going to dive directly to the changes you needed.
https://pub.dev/packages/firebase_messaging
After setting up the flutter environment as the documentation says you have to set a topic to subscribe for that notification.
Here inline 50 in main.dart file, I have subscribed to a topic called 'News' which is the topic which I need to push messages in the cloud function.
Then in your cloud functions, you have to send the message like following.
First, create a separate function which can send push notifications. Here also you need to set the same topic as you specified in the Flutter code. So as per this example, it is "News"
Then you just need to call this function from where you need to trigger this action like following
Then you will see the push notification in you app when you run the app. Hope this article will help someone in this push notification integration Thank you.
I hope you have done the basic setup in the flutter app as firebase_messaging documentation says as below. No need to repeat the same thing so I'm going to dive directly to the changes you needed.
https://pub.dev/packages/firebase_messaging
After setting up the flutter environment as the documentation says you have to set a topic to subscribe for that notification.
Here inline 50 in main.dart file, I have subscribed to a topic called 'News' which is the topic which I need to push messages in the cloud function.
Then in your cloud functions, you have to send the message like following.
First, create a separate function which can send push notifications. Here also you need to set the same topic as you specified in the Flutter code. So as per this example, it is "News"
function notificationSend(body: any){
const payload = {notification: {
title: 'test',
body: body
}};
admin.messaging().sendToTopic("News",payload)
.then(function(response){
console.log('Notification sent successfully:',response);
})
.catch(function(error){
console.log('Notification sent failed:',error);
});
}
Then you just need to call this function from where you need to trigger this action like following
notificationSend("Alert message is sending");
Then you will see the push notification in you app when you run the app. Hope this article will help someone in this push notification integration Thank you.
Comments
Post a Comment
Comment