【发布时间】:2021-05-04 00:25:43
【问题描述】:
我的 Flutter 应用程序遇到了问题,当它被最小化或手机睡眠时间过长(约 5 分钟以上)时,应用程序停止侦听 Firestore 更改。我遇到的一种解决方案是发送推送通知以重新唤醒设备。虽然它似乎已经修复了最小化问题(应用程序现在响应更改),但它仍然存在睡眠问题。我注意到该应用程序仍会收到推送通知,但在收到推送通知时屏幕没有亮起。这可能是为什么?我可以做些什么来强制应用程序连接到互联网?我正在尝试考虑一种解决方案,例如发送数据有效负载以在本地更改数据,但我不确定这是否是最佳方法(或者它是否会起作用)。我将发布我的 firebase 云功能,用于在文档更新时发送消息:
exports.sendLobbyNotificationTest = functions.firestore
.document("Lobbies/{lobbyCode}")
.onUpdate((change) => {
console.log("Checking for need of push notification...");
// const oldValue = change.before.data() || {};
const newValue = change.after.data() || {};
if (newValue.pushNotification == true) {
console.log("Found a push notification request for: ",
newValue.lobbyCode, "!");
// Set lobby back to false
admin.firestore().collection("Lobbies")
.doc(newValue.lobbyCode).update({
pushNotification: false,
});
return admin.messaging().sendToTopic(newValue.lobbyCode, message)
.then((result) => {
console.log("Message sent successfully: ", result);
// usersRef.where("lobbyCode", "==", newValue.lobbyCode).get()
// .then(function(querySnapshot) {
// querySnapshot.forEach(function(doc){
// })
// })
}).catch((err) => {
console.error("Error sending message: ", err);
});
}
console.log("No message needs to be sent!");
// return dummy value to prevent error
return 0;
});
const message = {
notification: {
title: "Bzzzt!",
body: "You've been buzzed!",
},
};
我有什么遗漏吗?
更新:我认为它只是因为手机被锁定而无法工作,一旦解锁它就开始正常工作。
【问题讨论】:
标签: android firebase flutter google-cloud-firestore google-cloud-functions