[英]How to send a fcm notification using javascript cloud functions in flutter?
I've created an app to test push notifications in a flutter app.我创建了一个应用程序来测试 Flutter 应用程序中的推送通知。
I am able to send a notification from the firebase messaging console and I can receive it as well in the foreground and background.我可以从 firebase 消息控制台发送通知,我也可以在前台和后台接收它。
Once I've done this, I moved to the next step which is to send it automatically using the firebase cloud messaging service and I've used javascript and I've deployed function and it gets executed without any problem.完成此操作后,我进入下一步,即使用 firebase 云消息传递服务自动发送它,我使用了 javascript 并部署了函数,并且它可以毫无问题地执行。
But the problem is that I can't receive a notification like this:-但问题是我无法收到这样的通知:-
but when I open my app since I've configured the firebase messaging inside initState();但是当我打开我的应用程序时,因为我已经在 initState() 中配置了 firebase 消息; I can see the notification and data get printed as well but I can't receive it like the photo above.
我可以看到通知和数据也被打印出来,但我不能像上面的照片那样接收它。
What should I do?我该怎么办?
try to have a background handler or a top-level method as firebase messaging plugin's read me file says.尝试使用后台处理程序或顶级方法,如 firebase 消息传递插件的自述文件所述。
About my javascript index.js file:关于我的 javascript index.js 文件:
It sends a notification for all the tokens in the pushTokens collection when a new document added to the posts collection and it does this but the problem is that I've mentioned above.当一个新文档添加到帖子集合时,它会为 pushTokens 集合中的所有令牌发送一个通知,它会这样做,但问题是我在上面提到过。
Index.js:索引.js:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var notificationMessageData;
exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot, context) => {
const notificationMessageData = snapshot.data();
return admin.firestore().collection('pushTokens').get()
.then(snapshot => {
var tokens = [];
if (snapshot.empty) {
console.log('No Devices');
throw new Error('No Devices');
} else {
for (var token of snapshot.docs) {
tokens.push(token.data().tokenID);
}
var payload = {
"notification": {
"title": "from" + notificationMessageData.writer,
"body": "from" + notificationMessageData.name,
"sound": "default"
},
"data": {
"sendername": notificationMessageData.writer,
"message": notificationMessageData.name
}
}
return admin.messaging().sendToDevice(tokens, payload)
}
})
.catch((err) => {
console.log(err);
return null;
})
});
Put your app in background and try sending notification again bcoz flutter firebase messaging plug-in will not create notification if you are using the app while the notification has been sent although you can see its data in but to generate notification you have to do it manually if user is currently using the app.将您的应用程序置于后台并再次尝试发送通知 bcoz flutter firebase 消息传递插件将不会创建通知,如果您在发送通知时使用该应用程序,尽管您可以看到其数据,但要生成通知,您必须手动执行如果用户当前正在使用该应用程序。
On the other cases plug-in will create notification like if app is terminated or its in background.在其他情况下,插件将创建通知,例如应用程序是否终止或其在后台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.