简体   繁体   English

通过GCM在渐进式Web应用程序中推送通知

[英]Push notifications in Progressive web app through GCM

while sending push notification i got ( Uncaught (in promise) ReferenceError: require is not defined(…)) error.here is my code 在发送推送通知时,我收到了(未捕获(承诺)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);
    }
  });
      })
    })
}

Screenshot of error enter image description here 错误的屏幕快照,请在此处输入图片说明

This code uses require , so it looks to me like you're trying to use node code in the browser. 这段代码使用了require ,所以在我看来,您在尝试在浏览器中使用节点代码。 To do that you'll need to use something like Browserify , although I'm not sure that's going to work for node-gcm as it may have certain requirements about sending network requests without cross origin restrictions etc. 为此,您将需要使用类似Browserify之类的方法 ,尽管我不确定该方法是否适用于node-gcm因为它可能对发送网络请求有一定的要求,而没有跨源限制等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM