if语句Firebase函数的语法错误

时间:2018-10-02 10:55:55

标签: javascript firebase google-cloud-functions

我正在尝试解决通知图标问题。高于android版本26的一种特定类型的图标,而低于26版本的另一种图标... 但是我不知道如何解决这个语法错误。顺便说一下,获取android.Build号没有问题。 Firebase也会正确记录它。

语法错误所在:if(sdknumber>26){
代码是:

const functions = require('firebase-functions'); 
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification =functions.database.ref('/notifyList/{receiver_id}/{sdk_number}/{notification_id}')
    .onWrite((change,context) =>
{
        const receiver_id = context.params.receiver_id;
        const sdk_number = context.params.sdk_number;
        const notification_id = context.params.notification_id;
        console.log('Notification to send: ',receiver_id);
        console.log('SDK Number: ',sdk_number);
        const sdknumber = Integer.parseInt(sdk_number);

        //const afterData = change.after.val();

        if(!change.after.exists()) 
        {
          return console.log('notification deleted from DB :',notification_id);
          return null;          
        }
    const deviceToken = admin.database().ref(`/Messenger/userlist/${receiver_id}/tokenID`).once('value');

    return deviceToken.then(result =>
    {
       const token_id = result.val();
       const payload = 
       {
            if(sdknumber>26){
                notification:
                {
               title: "New message!",
               body: "Tab to read it!",
               sound: "default",
               click_action: "OPENNOTIFICATION",
               icon: "rateiconsari"               
                }                   
            }else{
                notification:
                {
               title: "New message!",
               body: "Tab to read it!",
               sound: "default",
               click_action: "OPENNOTIFICATION",
               icon: "logo2"                  
                }                       
            }

       };

       return admin.messaging().sendToDevice(token_id,payload)
                .then(response =>
                {
                    console.log('notification sent');
                });
    });

    });

1 个答案:

答案 0 :(得分:0)

您的Object定义了文字JavaScript对象,您不能在这样的对象声明中放置payload语句。

您可以使用if运算符:

?:

或者您可以在定义之外使用const payload = { notification: { title: "New message!", body: "Tab to read it!", sound: "default", click_action: "OPENNOTIFICATION", icon: sdknumber > 26 ? "rateiconsari" : "logo2" } }; 来根据SDK版本修改if/else

payload

请注意,这是一个纯JavaScript语法问题,与Android无关。