通过GCM在Progressive Web应用程序中推送通知

在发送推送通知的时候我得到了(Uncaught(承诺)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); } }); }) }) } 

错误的屏幕截图在此输入图像描述

这段代码使用require ,所以在我看来你试图在浏览器中使用节点代码。 要做到这一点,你需要使用像Browserify这样的东西 ,虽然我不确定它是否适用于node-gcm因为它可能有一些关于发送网络请求而没有交叉来源限制等的要求。