Firebase admin SDK FCM error Exactly one of topic, token or condition is required
Asked Answered
C

2

23

I am fixing an error message which occurred, but it also used to work before. I am sending FCM notification with multiple tokens and am getting the following error

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> 

The code producing the error:

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 };
  }
}

Thoughts on what could be wrong?

Callous answered 11/2, 2020 at 13:32 Comment(1)
Same happening with meAgee
A
35

Solution - Avoid sending empty fcm token to Firebase.

I got the solution. I listed all my firebase token and there were some empty tokens in my database which was the main problem.

When we send empty fcm token, Firebase gives us below error

Exactly one of topic, token or condition is required
Agee answered 11/2, 2020 at 14:51 Comment(4)
Yeah, I think that's the answer. Our app had been through a rework and apparently some users' tokens got deleted. Checked and yeah, they were null :)Callous
Same happening but I don't have empty fcm tokenSible
Kindly check it once again, by printing every token. It could be empty or null.Agee
In my case, I was using send instead of sendMulticastMajor
P
8

Like the comment mentioned by @vijayst. My Issue was that I was using send(message) and including in my message an array of tokens. However, send() doesn't accept that. Send could accept a "topic", but to send to an array of tokens, you need to use sendMulticast(message)

send

sendMulticast

Preparative answered 4/10, 2021 at 14:26 Comment(1)
we have to use send, sendMulticast properly. firebase.google.com/docs/cloud-messaging/send-messageAppointed

© 2022 - 2024 — McMap. All rights reserved.