【发布时间】:2017-11-17 05:12:54
【问题描述】:
我有一个基于 apache cordova 构建的 android 应用程序。我想要此应用程序的推送通知。我使用 Visual Studio 2015 IDE 构建了这个应用程序。 我已经为此应用程序安装了一些插件,例如 cordova-plugin-device-orientation、状态栏等。为了为这个应用程序启用推送通知,我安装了 phonegap-plugin-push(2.1.0 版)并为我的应用程序提供了发件人 ID我使用 Firebase 创建的。通过创建新项目,添加 android 应用程序,下载 google-services.json 文件并将其放入我的项目中。
<script>
function setupPush() {
//This line below throws error on simulator saying PushNotification //is not defined
var push = PushNotification.init({
"android": {
"senderID": "XXXXXXXXXX"
},
"ios": {
"sound": true,
"alert": true,
"badge": true
},
"windows": {}
});
alert('registered');
push.on('registration', function (data) {
alert('registration event' + data.registrationId);
console.log("registration event: " + data.registrationId);
var oldRegId = localStorage.getItem('registrationId');
if (oldRegId !== data.registrationId) {
// Save new registration ID
alert('registration event' + data.registrationId)
localStorage.setItem('registrationId', data.registrationId);
// Post registrationId to your app server as the value has changed
}
});
push.on('error', function (e) {
console.log("push error = " + e.message);
});
}
$(document).on('deviceready', function deviceIsReady() {
console.log('Device is ready!');
setupPush();
});
</script>
我找到了这个代码here。
【问题讨论】:
标签: cordova firebase visual-studio-2015 phonegap-pushplugin apache-cordova