因此,我正在尝试使用Firebase-cloud-functions在我的Flutter应用中实现自动推送通知功能。
我的Firebase控制台出现此错误:
Error: Deadline exceeded
at Http2CallStream.call.on (/srv/node_modules/@grpc/grpc-js/build/src/call.js:68:41)
at emitOne (events.js:121:20)
at Http2CallStream.emit (events.js:211:7)
at process.nextTick (/srv/node_modules/@grpc/grpc-js/build/src/call-stream.js:71:22)
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
这是我的Firebase函数的index.js:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var msgData
exports.offerTrigger = functions.firestore.document(
'message/{messageID}'
).onCreate((snapshot, context) => {
msgData = snapshot.data();
admin.firestore().collection('pushtokens').get().then(async (snapshots) => {
var tokens = [];
if (snapshots.empty) {
console.log('No Devies');
} else {
for (var token of snapshots.docs) {
tokens.push(token.data().devtoken);
}
var payload = {
"notification": {
"title": msgData.buissnessName,
"body": msgData.messageValue,
"sound": "default"
},
"data": {
"sendername": msgData.buissnessName,
"message": msgData.messageValue,
}
}
try {
const response = await admin.messaging().sendToDevice(tokens, payload);
console.log('Pushed them all');
}
catch (err) {
console.log(err);
}
}
})
})
这就是令牌保存在Firestore中的方式:
这是我尝试发送推送通知的方式。但是没有用户收到它。