Firebase admin SDK FCM 错误 需要主题、令牌或条件之一

Pun*_*ish 8 javascript node.js firebase firebase-cloud-messaging firebase-admin

我正在修复发生的错误消息,但它以前也可以使用。我正在发送带有多个令牌的 FCM 通知并收到以下错误

0|api      | 2020-2-11 13:26:26   [ExceptionsHandler] Exactly one of topic, token or condition is required
0|api      |  +542752ms
0|api      | Error: Exactly one of topic, token or condition is required
0|api      |     at FirebaseMessagingError.FirebaseError [as constructor] (/var/www/tokee-api-new/node_modules/firebase-admin/lib/utils/error.js:42:28)
0|api      |     at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/var/www/tokee-api-new/node_modules/firebase-admin/lib/utils/error.js:88:28)
0|api      |     at new FirebaseMessagingError (/var/www/tokee-api-new/node_modules/firebase-admin/lib/utils/error.js:254:16)        0|api      |     at Object.validateMessage (/var/www/tokee-api-new/node_modules/firebase-admin/lib/messaging/messaging-types.js:46:15)
0|api      |     at /var/www/tokee-api-new/node_modules/firebase-admin/lib/messaging/messaging.js:265:31
0|api      |     at Array.map (<anonymous>)
0|api      |     at Messaging.sendAll (/var/www/tokee-api-new/node_modules/firebase-admin/lib/messaging/messaging.js:264:29)
0|api      |     at Messaging.sendMulticast (/var/www/tokee-api-new/node_modules/firebase-admin/lib/messaging/messaging.js:313:21)   0|api      |     at userChunks.forEach.userChunk (/var/www/tokee-api-new/src/modules/common/firebase/firebase.service.ts:154:42)     0|api      |     at Array.forEach (<anonymous>)
0|api      |     at FCMService.<anonymous> (/var/www/tokee-api-new/src/modules/common/firebase/firebase.service.ts:142:16)
0|api      |     at Generator.next (<anonymous>)
0|api      |     at /var/www/tokee-api-new/dist/src/modules/common/firebase/firebase.service.js:13:71
0|api      |     at new Promise (<anonymous>)
0|api      |     at __awaiter (/var/www/tokee-api-new/dist/src/modules/common/firebase/firebase.service.js:9:12)
0|api      |     at FCMService.sendToMany (/var/www/tokee-api-new/dist/src/modules/common/firebase/firebase.service.js:123:16)       0|api      |     at AdminNotificationController.<anonymous> (/var/www/tokee-api-new/src/modules/notification/admin-notification.controller.ts:65:42)
0|api      |     at Generator.next (<anonymous>)
0|api      |     at fulfilled (/var/www/tokee-api-new/dist/src/modules/notification/admin-notification.controller.js:16:58)
0|api      |     at <anonymous> 
Run Code Online (Sandbox Code Playgroud)

产生错误的代码:

async sendToMany(users: User[], notification: VSendNotification, title = 'Title', lowPriority = false) {
    // We split emails into arrays of max. 100 size because of the limitation with FCM
    const userChunks = _.chunk(users, 100);
    const promises = [];
    userChunks.forEach(userChunk => {
      const msg: admin.messaging.MulticastMessage = {
        notification: {
          title,
          body: notification.body
        },
        tokens: userChunk.map(user => user.firebaseToken),
        android: {
          priority: lowPriority ? 'normal' : 'high'
        }
      };
      promises.push(this.app.messaging().sendMulticast(msg));
    });
    const result = await Promise.all(promises);
    let accepted = 0;
    let rejected = 0;
    result.forEach(response => {
      accepted += response.successCount;
      rejected += response.failureCount;
    });

    return { accepted, rejected, notification };
  }
}
Run Code Online (Sandbox Code Playgroud)

关于什么可能是错误的想法?

Var*_*kar 16

解决方案- 避免向 Firebase 发送空的 fcm 令牌。

我得到了解决方案。我列出了我所有的 firebase 令牌,我的数据库中有一些空令牌,这是主要问题。

当我们发送空的 fcm 令牌时,Firebase 会给我们以下错误

Exactly one of topic, token or condition is required
Run Code Online (Sandbox Code Playgroud)

  • 同样的情况发生,但我没有空的 fcm 令牌 (4认同)
  • 是的,我想这就是答案。我们的应用程序经过了返工,显然一些用户的令牌被删除了。检查了一下,是的,它们是空的:) (2认同)
  • 就我而言,我使用的是 send 而不是 sendMulticast (2认同)

Sea*_*nMC 7

就像@vijayst 提到的评论一样。我的问题是我正在使用 send(message) 并在消息中包含tokens. 但是,send() 不接受这一点。Send 可以接受一个“主题”,但要发送到令牌数组,您需要使用 sendMulticast(message)

发送

发送组播