Phonegap IOS - 插件推送设置 [英] Phonegap IOS - Plugin Push Setup

查看:19
本文介绍了Phonegap IOS - 插件推送设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

var app = {//应用程序构造器初始化:函数(){this.bindEvents();},//绑定事件监听器////绑定启动时需要的任何事件.常见事件有://加载"、设备就绪"、离线"和在线".绑定事件:函数(){document.addEventListener('deviceready', this.onDeviceReady, false);},//设备就绪事件处理程序////'this' 的作用域是事件.为了调用'receivedEvent'//函数,我们必须显式调用'app.receivedEvent(...);'onDeviceReady: 函数 () {app.receivedEvent('deviceready');var push = PushNotification.init({安卓": {发件人ID":1234567890"},"ios": { "alert": "true", "badge": "true", "sound": "true" },窗口":{}});push.on('注册', 函数(数据){console.log("注册事件");//document.getElementById("regId").innerHTML = data.registrationId;警报(data.registrationId)控制台日志(JSON.stringify(数据));});push.on('通知', 函数(数据){console.log("通知事件");控制台日志(JSON.stringify(数据));var card = document.getElementById("cards");var card = '

'+'<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>';card.innerHTML += 卡片;push.finish(函数(){console.log('完成调用成功');});});push.on('错误', 函数 (e) {console.log("推送错误");});},//在收到的事件上更新 DOM接收事件:函数(id){var parentElement = document.getElementById(id);var listenElement = parentElement.querySelector('.listening');var receivedElement = parentElement.querySelector('.received');listenElement.setAttribute('style', 'display:none;');receivedElement.setAttribute('style', 'display:block;');console.log('收到的事件:' + id);}};函数成功处理程序(结果){alert('结果 = ' + 结果);//在这里你会得到你的设备ID.}函数错误处理程序(错误){alert('错误 = ' + 错误);}

所以,我不确定从这里确切地去哪里.我已经使用推送向导设置了一个帐户并创建了其他证书,但这并没有选择任何设备,因为我认为还没有设置为接收通知的设备.

所以我想我的主要问题是,我是否错过了一些愚蠢的事情,比如注册阶段,我的设备注册为接收通知?

解决方案

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

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

快速解决方法是使用 app.onDeviceReady 而不是 this.onDeviceReady 您可以通过使您的 onDeviceReady() 一个全局函数,并保留 this .

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

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 get a device to even be registered at all. I've added the following plugin to my project (added in the config for phonegap build) https://github.com/phonegap/phonegap-plugin-push

I added the plugin and have following in my javascript file.

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

So, I'm not sure where to go from here exactly. I've set up an account with push wizard and have created the additional certificates, but that is not picking up any devices as I assume there is no devices that are set to receive notifications yet.

So I guess my main issue is, am I missing something stupid like a registration stage, where my device is registered to receive notifications?

解决方案

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.

这篇关于Phonegap IOS - 插件推送设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆