1

在发送推送通知时我得到了(未捕获(承诺中)ReferenceError:未定义要求(...))错误。这是我的代码

 const endPoint = subscription.endpoint.slice(subscription.endpoint.lastIndexOf('/')+1);
console.log(endPoint);
var gcm = require('node-gcm');
var message = new gcm.Message({
    notification: {
        title: "Hello, World",
        icon: "ic_launcher",
        body: "This is a notification that will be displayed ASAP.",
        tag:"hello"
    }
});

var regTokens = [endPoint];
  var sender = new gcm.Sender('AIzaSyD9Bcxd_MQZFoGjO1y_hPm-xUdgnM25Ny4'); //API Key
  // Now the sender can be used to send messages
  sender.send(message, { registrationTokens: regTokens }, function (error, response) {
   if (error) {
      console.error(error);
      res.status(400);
    }
   else {
      console.log(response);
      res.status(200);
    }
  });
      })
    })
}

错误截图在 此处输入图像描述

4

1 回答 1

2

此代码使用require,因此在我看来您正尝试在浏览器中使用节点代码。为此,您需要使用Browserify 之类的东西,尽管我不确定这是否可行,node-gcm因为它可能对发送没有跨源限制的网络请求等有某些要求。

于 2016-04-18T17:47:24.033 回答