繁体   English   中英

Phonegap IOS-插件推送设置

[英]Phonegap IOS - Plugin Push Setup

我正在使用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是在运行时解决的,而不是在汇编时 (或编译时 )解决的。 事件触发时,由于您的app对象现在不在范围内, this将解决全局this 该事件在您的app对象外部触发。

速战速决是做app.onDeviceReady而不是this.onDeviceReady您可以通过测试这个onDeviceReady()全局函数和离开this到位。

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

因此,有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)
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM