Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过GCM在渐进式web应用中推送通知_Javascript_Jquery_Ajax_Push Notification_Progressive Web Apps - Fatal编程技术网

Javascript 通过GCM在渐进式web应用中推送通知

Javascript 通过GCM在渐进式web应用中推送通知,javascript,jquery,ajax,push-notification,progressive-web-apps,Javascript,Jquery,Ajax,Push Notification,Progressive Web Apps,发送推送通知时,我收到了(未捕获(承诺中)ReferenceError:require未定义(…)错误。这是我的代码 const endPoint = subscription.endpoint.slice(subscription.endpoint.lastIndexOf('/')+1); console.log(endPoint); var gcm = require('node-gcm'); var message = new gcm.Message({ notification

发送推送通知时,我收到了(未捕获(承诺中)ReferenceError:require未定义(…)错误。这是我的代码

 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
,因此在我看来,您似乎在尝试在浏览器中使用节点代码。要做到这一点,您需要使用类似的东西,尽管我不确定这是否适用于
节点gcm
,因为它可能对发送网络请求没有跨源限制等有某些要求。

您不能在浏览器中使用
require
。如果你喜欢混合应用程序开发,你需要安装插件PushNotification,然后你可以在这里进一步查看文档
https://github.com/phonegap-build/PushPlugin.git