Phonegap IOS - 插件推送安装 [英] Phonegap IOS - Plugin Push Setup
问题描述
我正在使用Phonegap和Phonegap Build开发应用程序。该应用程序已经完成了,但我不是在推送通知。
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.
我一直在努力得到一个设备,甚至被注册。我已将以下插件添加到我的项目中(在phonegap构建的配置中添加) https:// github.com/phonegap/phonegap-plugin-push
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
我添加了插件,并在我的javascript文件中有以下内容。
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?
推荐答案
您未正确使用此
上下文。这是一个常见的错误。 Javascript 这不像Java
这个
工作。
You are not using the this
context correctly. This is a common mistake. The Javascript this
does NOT work like the Java this
.
它不工作的原因是因为而不是装配时解决了 this
(或编译时)。当事件触发时,此
解析为全局此
,因为您的 app
对象现在超出范围。事件触发您的应用程式
物件之外的*外部。
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.
快速解决方法是执行 app.onDeviceReady
而不是 onDeviceReady()一个全局函数,并保留 this.onDeviceReady
this
。
A quick fix would be to do app.onDeviceReady
instead of You can test this by making yourthis.onDeviceReady
onDeviceReady()
a global function and leaving the this
in place.
应该有所帮助。 – Best of Luck。
These videos should help. – Best of Luck.
JavaScript中的上下文 - 2/4 - JavaScript如何确定这个实际上是什么
JavaScript中的上下文 - 3/4 - 此可能不是你期待&如何解决
JavaScript中的上下文 - 4/4 - 掌握这:其他技术&未来支持
这篇关于Phonegap IOS - 插件推送安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!