发送推送通知时收到(未捕获(in promise)未定义ReferenceError: require (…))error.here是我的代码
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);
}
});
})
})
}error enter image description here的屏幕快照
发布于 2016-04-19 01:47:24
这段代码使用require,所以在我看来,您正在尝试使用浏览器中的节点代码。要做到这一点,你需要使用像Browserify这样的东西,尽管我不确定这是否适用于node-gcm,因为它可能对发送没有跨域限制的网络请求有一定的要求。
https://stackoverflow.com/questions/36619584
复制相似问题