首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过GCM在渐进式web应用程序中推送通知

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

Stack Overflow用户
提问于 2016-04-14 17:54:42
回答 1查看 507关注 0票数 1

发送推送通知时收到(未捕获(in promise)未定义ReferenceError: require (…))error.here是我的代码

代码语言:javascript
复制
 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的屏幕快照

EN

回答 1

Stack Overflow用户

发布于 2016-04-19 01:47:24

这段代码使用require,所以在我看来,您正在尝试使用浏览器中的节点代码。要做到这一点,你需要使用像Browserify这样的东西,尽管我不确定这是否适用于node-gcm,因为它可能对发送没有跨域限制的网络请求有一定的要求。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36619584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档