繁体   English   中英

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

[英]Firebase notification for Flutter application suddenly stopped

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

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

我的 function 登录 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);
      }
    });
 }
});

我正在使用实时数据库,其中针对连接到系统的任何设备保存设备令牌。 我有一份名为DeviceTokens的文件。 我有另一个存储通知的文档通知 当在通知中生成一个条目时,function 应该识别它,然后遍历所有设备令牌并将该通知发送到订阅该通知的所有设备。

我的 firebase 控制台日志打印通知已成功发送,用于 DeviceTokens 中存在的设备令牌数。 但是在执行 function 后显示错误。

 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 回购协议,了解如何处理通知https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js

尝试检查您的规则,也许它们已过期。 如果这也很好,我认为问题可能出在新更新上,现在您必须在执行任何操作之前初始化 Firebase 应用程序。 您必须将 firebase 核心添加到 pubsec 文件,然后在任何 firebase 操作之前只需导入 firebase 核心并通过以下方式初始化

await Firebase.initializeApp();

也许这会奏效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM