【问题标题】:Push notifications in Progressive web app through GCM通过 GCM 在 Progressive Web 应用程序中推送通知
【发布时间】:2016-04-14 09:54:41
【问题描述】:

在发送推送通知时,我收到了(未捕获(承诺中)ReferenceError: require is not defined(...)) 错误。这是我的代码

 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);
    }
  });
      })
    })
}

错误截图 enter image description here

【问题讨论】:

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

标签: javascript jquery ajax push-notification progressive-web-apps


【解决方案1】:

此代码使用require,因此在我看来,您正在尝试在浏览器中使用节点代码。为此,您需要使用Browserify 之类的东西,尽管我不确定这是否适用于node-gcm,因为它可能对发送没有跨源限制的网络请求等有某些要求。

【讨论】:

    猜你喜欢
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 2014-05-19
    • 1970-01-01
    • 2015-03-08
    • 2017-01-30
    相关资源
    最近更新 更多