Flutter应用程序的Firebase通知突然停止



我正在尝试将FCM集成到我的颤振应用程序中。我已经为android和iOS完成了所有的连接设置。当我在云消息测试通知功能的帮助下发送通知时,通知会在设备中正确接收。

我已经编写了一个函数脚本,它在给定的时间自动发送通知。大约两周前,当我部署该功能时,它几乎像预期的那样运行大多数客户端都收到了应有的通知。然而,在过去的几天里,通知已经完全停止到达客户端。

我在firebase控制台中的功能日志表明通知已经发送,但报告显示我的所有用户都没有收到通知。

我确信我写有效载荷的方式有问题。

这是我的代码

exports.sendFollowerNotification = functions.database.ref('/notifications/{notificationID}')
.onCreate(async (snapshot, context) => {
const notificationID = context.params.notificationID;
const notificationData = snapshot.val();
const getDeviceTokensPromise = admin.database()
.ref('/DeviceTokens').orderByChild("subscribed").equalTo("1").once('value');

//list all tokens in a array
let tokensSnapshot;
let tokens;
const results = await Promise.all([getDeviceTokensPromise]);
tokensSnapshot = results[0];
if (!tokensSnapshot.hasChildren()) {
return console.log('There are no notification tokens to send to.');
}
console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
tokens = Object.keys(tokensSnapshot.val());
const payload = {
notification: {
body: notificationData.body,
title: notificationData.title,
click_action: 'FLUTTER_NOTIFICATION_CLICK'
},
data: {
id: '1', 
status: 'done'
},
};
//loop through each element in the /DeviceTokens node and get data for each entry with the 
getTheTokens function.
for (var tokenDetails in tokens) {
getTheTokens(tokens[tokenDetails]);
}
//getTheTokens function
function getTheTokens() {
var refKey = admin.database().ref('/DeviceTokens').child(tokens[tokenDetails]);
refKey.once('value', (snapshot) => {
var theToken = snapshot.child('/device_token').val();
try {
const response = admin.messaging().sendToDevice(theToken, payload);
// gives proper feedback to console.
console.log('Notification sent successfully');
} catch (err) {
console.log(err);
}
});
}
});

我使用的是RealTime数据库,其中会针对连接到系统的任何设备保存设备令牌。我有一个名为DeviceTokens的文档。我有另一个通知文档,其中存储了通知。当通知中生成条目时,该函数应该识别该条目,然后循环遍历所有设备令牌,并将该通知发送给订阅该通知的所有设备。

我的firebase控制台日志为DeviceTokens中存在的设备令牌数量打印成功发送的通知。但是,在执行函数后,它会显示一个错误。

sendFollowerNotification
Error: Error while making request: Client network socket disconnected before secure TLS connection was established. 
Error code: ECONNRESET at FirebaseAppError.FirebaseError [as constructor (/workspace/node_modules/firebase-admin/lib/utils/error.js:42:28) 
at FirebaseAppError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:88:28) 
at new FirebaseAppError (/workspace/node_modules/firebase-admin/lib/utils/error.js:123:28)
at /workspace/node_modules/firebase-admin/lib/utils/api-request.js:209:19 
at process._tickCallback (internal/process/next_tick.js:68:7) 

sendFollowerNotification
Error: Process exited with code 16
at process.on.code (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:271:38)
at process.emit (events.js:198:13)
at process.EventEmitter.emit (domain.js:448:20)
at process.exit (internal/process/per_thread.js:168:15)
at Object.logAndSendError (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:37:9)
at process.on.err (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:268:22)
at process.emit (events.js:198:13)
at process.EventEmitter.emit (domain.js:448:20)
at emitPromiseRejectionWarnings (internal/process/promises.js:140:18)
at process._tickCallback (internal/process/next_tick.js:69:34) 

我该如何解决这个问题?

次要提示:

如果它有助于您的情况检查Firebase repo如何处理通知https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js

尝试检查您的规则是否已过期。如果这也可以的话,我认为问题可能是新的更新,现在你必须在进行任何操作之前初始化Firebase应用程序。你必须将firebase核心添加到pubsec文件中,然后在进行任何firebase操作之前,只需导入firebase核心并通过进行初始化

await Firebase.initializeApp();

也许这会奏效。

最新更新