是否有办法从c#向Flutter应用程序发送云消息或推送通知?



对于我的用例,用户从c# WinForm桌面应用程序生成一些数据(创建一个名为" request"的集合中的新文档);在Firestore)。我想要的是,每当在请求集合中创建/添加新文档时,都会向Flutter应用程序发送通知。我正在寻找使用c#解决此问题的一些指导方针。

我假设您已经将c#软件连接到firebase作为后端。如果是这样,则:

1 -首先将你的flutter应用连接到firebase。点击此链接:将flutter应用程序连接到firebase

2 -使用此链接学习并在firebase上创建您的第一个云功能。从flutter应用程序终端创建第一个云功能

3 -现在您可以创建一个云函数,在请求集合中创建文档时,将使用该函数向您想要的设备生成通知

4 - 部署此函数 将通知发送到所需的设备。

exports.sendFirstNotification =
functions.firestore.document('/Requests/{documentId}')
.onCreate(async (snap, context) => {
var name = "Bilal Saeed";
var myDeviceToken = snap.data().fcmToken;
// You need this token for push notification on a device.
// Each device has its own token.
// Access it in this function. 
//If you are working on C# software then create your token from flutter app
//and save it on firestore. And access that token here and save it in myDeviceToken variable.
// Currently in this function its assumed that, the newly document created //in your Request collection contains the device token which is being //assigned to myDeviceToken variable.
await admin.messaging().send({
token: myDeviceToken,
notification: {
title: `Hi, you received first notification by the help of ${name}`,
body: " Upvote his answer if he really helped you!"
},

}).then(value => {
functions.logger.log("First notification sent to the device");
}).catch((e) =>{
functions.logger.log(e.toString());
});
});

从终端使用这个命令来部署这个函数。

firebase deploy --only functions:sendFirstNotification

现在从您的c#软件创建文档,您将在您的flutter应用程序移动设备上获得关于在请求集合中创建文档的通知。恭喜!

最新更新