通过GCM在Progressive web应用程序中推送通知
Push notifications in Progressive web app through GCM
在发送推送通知时,我收到了(Uncaught(in promise)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);
}
});
})
})
}
错误屏幕截图在此处输入图像描述
此代码使用require
,因此在我看来,您正试图在浏览器中使用节点代码。要做到这一点,您需要使用类似Browserify的东西,尽管我不确定这是否适用于node-gcm
,因为它可能对发送无跨源限制的网络请求有某些要求。
相关文章:
- Meteor应用程序上的警报/通知不会出现
- GCM推送通知,如果应用程序在手机中关闭(Phonegap Android)
- 在通知点击时使用Ion中的OneSignal插件路由到应用程序中的状态
- 是否可以使用推送通知Windows 8 Metro应用程序's使用Javascript
- 如何使用Quickblox在Cordova应用程序中实现推送通知支持
- 如何安全地获取&使用Facebook应用程序访问令牌发送通知使用PHP&Javascript
- Phonegap应用程序检查是否启用了推送通知
- 为什么在应用程序打开时收到通知时弹出UIAlertview(科尔多瓦)
- 点击通知时打开应用程序内的网址 - Onesignal,Phonegap
- 通过GCM在Progressive web应用程序中推送通知
- 点击通知后启动Firefox OS应用程序
- 当未读邮件通过Office 365应用程序(离子移动应用程序)时通知
- 聊天应用程序 - 当有人进入/离开房间时,用声音通知用户
- 如何创建应用程序独立通知服务器
- 应用程序邀请不会通知用户
- Facebook,通知用户我的应用程序更新
- Phonegap,Strophe.js和xmpp pubsub在应用程序离线时推送通知
- Phonegap android 向其他应用程序用户发送通知
- 使用javascript通知应用程序图标
- 实时通知应用程序DateTimeField问题