javascript - Cordova aerogear-push android推送未定义

标签 javascript android cordova push-notification aerogear

我正在尝试使用 aerogear 推送通知插件在 cordova 上构建一个简单的应用程序

我正在做的就是严格遵循本指南:https://aerogear.org/docs/guides/aerogear-cordova/AerogearCordovaPush/#_sample_example

但是,将示例代码放入我的js中后,这一行:

push.register(onNotification, successHandler, errorHandler, pushConfig);

由于未定义推送,将导致引用错误

我按照之前的所有步骤进行操作,aerogear-cordova-push 插件位于插件文件夹中,也许我需要一些额外的步骤来引用该插件?

此外,该插件在其文件夹内提供了一个 index.html 作为示例,但即使使用它,我也无法解析推送

我尝试将插件的js文件移动到www文件夹中,并在执行index.js之前将它们链接到索引上,因为这不是很正确,会导致其他引用错误

www文件夹中的index.html与标准cordova项目创建后提供的相同

这是我的index.js,我可以通过try catch在android上显示错误:

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
    try {
        app.receivedEvent('deviceready');
        var pushConfig = {
            pushServerURL: "...",
            android: {
                senderID: "...",
                variantID: "...",
                variantSecret: "..."
            }
        };
        push.register(app.onNotification, successHandler, errorHandler, pushConfig);
    }
    catch (e){
        alert(e);
    }


    function successHandler() {
        console.log('success')
    }

    function errorHandler(message) {
        console.log('error ' + message);
    }
},
onNotification: function(event) {
    alert(event.alert);
},

receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}};  app.initialize();

最佳答案

我解决了这个问题,因为插件没有正确安装在我的应用程序中,并且安装会失败,因为我缺少为 android 构建所需的 google-service.json 文件

关于javascript - Cordova aerogear-push android推送未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43372730/

相关文章:

javascript - 如何在 firefox 中注入(inject) javascript 代码?

javascript - 是否可以用 javascript 编写网络爬虫?

java - java中的空检查和监听器

android - 当应用程序处于空闲状态或处于后台时,如何获取 cordova android 推送通知?

javascript - 在 Angular 2 中绑定(bind) ngmodel 中的 json 值

javascript - 我如何限制在 disqus 评论框中显示评论

android - 如何防止EditText在标点符号后换行

Android - 如何让手机随着音乐播放而振动

javascript - 使用 navigator.notification.confirm 传递两个参数?

ios - Ionic (pluginName : statusbar) does not exist, can't change statusbar font color