Phonegap IOS - 插件推送设置

时间:2015-11-18 11:02:12

标签: 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);
}

所以,我不确定从哪里开始。我已经使用推送向导设置了一个帐户,并创建了其他证书,但这并没有提取任何设备,因为我认为没有设置为接收通知的设备。

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

2 个答案:

答案 0 :(得分:1)

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

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

快速解决方法是执行app.onDeviceReady而非 this.onDeviceReady 您可以通过将onDeviceReady()设为全局函数并离开{{1到位。

这些视频应有所帮助。 - 最好的运气。

答案 1 :(得分:0)

所以有2个部分,首先是jesseMonroy提到的,我的代码没有被正确选取,我的js文件没有正常运行。

第二部分是我没有调用注册函数,它允许你获得device_token。

        push.registerDevice({ alert: true, badge: true, sound: true }, function (status) {
            app.myLog.value += JSON.stringify(['registerDevice status: ', status]) + "\n";
            app.storeToken(status.deviceToken);
            alert(status.deviceToken)
        });

这就是我所缺少的。我的整个init部分看起来像这样;

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


        //  var pushNotification = window.plugins.PushNotification;
        push.registerDevice({ alert: true, badge: true, sound: true }, function (status) {
            app.myLog.value += JSON.stringify(['registerDevice status: ', status]) + "\n";
            app.storeToken(status.deviceToken);
            alert(status.deviceToken)
        });
相关问题