javascript - Phonegap IOS - 插件推送设置

标签 javascript ios cordova push-notification

我正在使用 Phonegap 和 Phonegap Build 开发应用程序。该应用程序已经基本完成,但我没有处理推送通知。

我正在努力让一台设备完全注册。我已经将以下插件添加到我的项目中(添加到 phonegap 构建的配置中)https://github.com/phonegap/phonegap-plugin-push

我添加了插件并在我的 javascript 文件中添加了以下内容。

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 () {
        app.receivedEvent('deviceready');

        var push = PushNotification.init({
            "android": {
                "senderID": "1234567890"
            },
            "ios": { "alert": "true", "badge": "true", "sound": "true" },
            "windows": {}
        });

        push.on('registration', function (data) {
            console.log("registration event");
            //document.getElementById("regId").innerHTML = data.registrationId;
            alert(data.registrationId)
            console.log(JSON.stringify(data));
        });

        push.on('notification', function (data) {
            console.log("notification event");
            console.log(JSON.stringify(data));
            var cards = document.getElementById("cards");
            var card = '<div class="row">' +
                  '<div class="col s12 m6">' +
                  '  <div class="card darken-1">' +
                  '    <div class="card-content black-text">' +
                  '      <span class="card-title black-text">' + data.title + '</span>' +
                  '      <p>' + data.message + '</p>' +
                  '    </div>' +
                  '  </div>' +
                  ' </div>' +
                  '</div>';
            cards.innerHTML += card;

            push.finish(function () {
                console.log('finish successfully called');
            });
        });

        push.on('error', function (e) {
            console.log("push error");
        });

    },
    // Update DOM on a Received Event
    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);
    }
};

function successHandler(result) {
    alert('result = ' + result); //Here you will get your device id.
}


function errorHandler(error) {
    alert('error = ' + error);
}

所以,我不确定从这里到哪里去。我已经使用推送向导设置了一个帐户并创建了额外的证书,但这并没有获取任何设备,因为我假设还没有设置为接收通知的设备。

所以我想我的主要问题是,我是否遗漏了一些愚蠢的东西,比如我的设备注册接收通知的注册阶段?

最佳答案

您没有正确使用 this 上下文。这是一个常见的错误。 Javascript this 不像 Java this 那样工作。

它不起作用的原因是 this运行时 得到解析,而不是汇编时(或编译时)。当事件触发时,this 解析为全局 this,因为您的 app 对象现在超出范围。该事件在您的 app 对象的*外部*触发。

一个快速解决方法是使用 app.onDeviceReady 而不是 this.onDeviceReady 你可以通过让你的onDeviceReady 来测试它() 一个全局函数并保留 this

这些视频应该有所帮助。 – 祝你好运。

关于javascript - Phonegap IOS - 插件推送设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33778334/

相关文章:

javascript - 如何将我的数据传递到 google 图表 api?

javascript - 带有 .p12 和 pem 证书的 Node.js POST 请求输出 "Error: read ECONNRESET"

javascript - Fastify 插件 fastify-autoload 错误插件必须是一个函数

ios - Xamarin iOS 自动布局 : Resize width and vertical scroll automatically for various devices while keeping the horizontal scroll disabled

ios - Cordova 2.8.1 : camera. getPicture with photolibarary source on ios

javascript - 即使设置焦点也无法在文本框中输入

iphone - OGL_ES 2.0 : Using multiple texture units vs rebinding textures for separate shaders

ios - 在 Interface Builder 中设置 UIButton 图层边框宽度和颜色

ios - 我无法从不同类上的函数调用 javascript

cordova - 如何使用 Azure 通知中心或 Azure 应用服务移动应用发送推送通知?