Phonegap IOS - Plugin Push Setup

前端 未结 2 795
遇见更好的自我
遇见更好的自我 2020-12-22 05:27

I\'m developing an App using Phonegap and Phonegap Build. The app has been pretty much completed but I\'m not working on the Push Notifications.

I\'m struggling to g

相关标签:
2条回答
  • 2020-12-22 06:03

    You are not using the this context correctly. This is a common mistake. The Javascript this does NOT work like the Java this.

    The reason it does not work is because the this gets resolved at run-time, not assemble-time (or compile-time). When the event fires, this resolves to the the global this because your app object is now out of scope. The event fires *outside* of your app object.

    A quick fix would be to do app.onDeviceReady instead of this.onDeviceReady You can test this by making youronDeviceReady() a global function and leaving the this in place.

    These videos should help. – Best of Luck.

    • Context in JavaScript - 1/4 - Purpose and Problems with JavaScript's "This"

    • Context in JavaScript - 2/4 - How JavaScript Decides What "This" Actually Is

    • Context in JavaScript - 3/4 - "This" May Not Be What You Expected & How to Fix It

    • Context in JavaScript - 4/4 - Mastering "This:" Additional Techniques & Future Support

    0 讨论(0)
  • 2020-12-22 06:19

    So there was 2 sections to this, First what jesseMonroy mentioned, my code wasn't being picked up properly and my js file wasn't being run properly.

    The second part was that I wasn't calling a register function, which allows you to get a 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)
            });
    

    This is what i was missing. my whole init section looks like this;

    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)
            });
    
    0 讨论(0)
提交回复
热议问题