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

Mil*_*l3r 5 push-notification firebase firebase-realtime-database flutter

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

我编写了一个函数脚本,它在给定时间自动发送通知。当我在大约 2 周前部署该功能时,它几乎按预期工作。大多数客户都收到了他们应该收到的通知。但是,过去几天通知已完全停止到达客户端。

我在 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);
      }
    });
 }
});
Run Code Online (Sandbox Code Playgroud)

我正在使用实时数据库,其中针对连接到系统的任何设备保存了设备令牌。我有一个名为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) 
Run Code Online (Sandbox Code Playgroud)

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) 
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Sha*_*ken 0

小提示:

如果它对您的情况有帮助,请查看 Firebase 存储库,了解如何处理通知 https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js