【问题标题】:What is the correct syntax to send FCM notification to multiple topics via condition using node.js?使用 node.js 通过条件向多个主题发送 FCM 通知的正确语法是什么?
【发布时间】:2019-02-17 11:13:52
【问题描述】:

我正在尝试将 fcm 通知从我的 node.js 服务器发送到 android 设备。它适用于单个主题,但在有多个主题时会失败。

注意:使用单个主题,例如,

var topic = 'MaleFemale'; // here i have only one topic 
                var message = {
                    android: {
                        ttl: 5*60*1000, // 5 min in milliseconds
                        priority: 'normal',
                        notification: {
                        title: 'Someone want to chat',
                        body: 'Tap to join chat',
                        icon: 'stock_ticker_update',
                        color: '#2C5AE1'
                        }
                    },
                    topic: topic
                };

                send.admin.messaging().send(message)
                .then((response) => {
                // Response is a message ID string.
                console.log('Successfully sent message:', response);
                })
                .catch((error) => {
                console.log('Error sending message:', error);
                });

注意:在多个主题中失败,例如,

var topic = "'FemaleAny' in topics || 'MaleAny' in topics";//here i have multiple topic 
                var message = {
                    android: {
                        ttl: 5*60*1000, // 5 min in milliseconds
                        priority: 'normal',
                        notification: {
                        title: 'Someone want to chat',
                        body: 'Tap to join chat',
                        icon: 'stock_ticker_update',
                        color: '#2C5AE1'
                        }
                    },
                    condition: topic
                };

                send.admin.messaging().sendToCondition(message)
                .then((response) => {
                // Response is a message ID string.
                console.log('Successfully sent message:', response);
                })
                .catch((error) => {
                console.log('Error sending message:', error);
                });

错误:

throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_RECIPIENT, 'Condition provided to sendToCondition() must be a non-empty string.');
            ^

Error: Condition provided to sendToCondition() must be a non-empty string.
    at FirebaseMessagingError.FirebaseError [as constructor] (/Users/kdblue/Desktop/Android Projects/ChatApp/server/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/Users/kdblue/Desktop/Android Projects/ChatApp/server/node_modules/firebase-admin/lib/utils/error.js:85:28)
    at new FirebaseMessagingError (/Users/kdblue/Desktop/Android Projects/ChatApp/server/node_modules/firebase-admin/lib/utils/error.js:241:16)
    at Messaging.sendToCondition (/Users/kdblue/Desktop/Android Projects/ChatApp/server/node_modules/firebase-admin/lib/messaging/messaging.js:637:19)
    at manageQueue (/Users/kdblue/Desktop/Android Projects/ChatApp/server/index.js:268:40)
    at Socket.socket.on (/Users/kdblue/Desktop/Android Projects/ChatApp/server/index.js:25:9)
    at Socket.emit (events.js:182:13)
    at /Users/kdblue/Desktop/Android Projects/ChatApp/server/node_modules/socket.io/lib/socket.js:528:12
    at process._tickCallback (internal/process/next_tick.js:61:11)

我试过了,但没有为我工作:-

1) https://firebase.google.com/docs/cloud-messaging/admin/send-messages?authuser=0

2) What is the correct syntax to send Firebase Cloud Messaging (FCM) to multiple topics via condition

【问题讨论】:

    标签: node.js


    【解决方案1】:

    这有点奇怪,我正在回答我的问题,无论如何 我通过将方法 sendToCondition() 更改为 send() 解决了问题。

    var condition = "'FemaleAny' in topics || 'MaleAny' in topics";
                    var message = {
                        android: {
                            ttl: 30*1000, //  30sec in milliseconds
                            priority: 'normal',
                            notification: {
                            title: 'Someone want to chat',
                            body: 'Tap to join chat',
                            icon: 'stock_ticker_update',
                            color: '#2C5AE1',
                            tag: '4'
                            }
                        },
                        condition: condition
                    };
    
                    send.admin.messaging().send(message)
                    .then((response) => {
                    // Response is a message ID string.
                    console.log('Successfully sent message:', response);
                    })
                    .catch((error) => {
                    console.log('Error sending message:', error);
                    });
    

    【讨论】:

      猜你喜欢
      • 2019-07-19
      • 2019-11-06
      • 2017-11-08
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多