我正在用Netbeans构建一个Cordova项目,它使用PushPlugin (https://github.com/phonegap-build/PushPlugin)。
它在Android上工作得很好,但在iOS上它不能注册设备。它也不会给出任何错误。
同样奇怪的是,插件文件夹(com.phonegap.plugins.PushPlugin)在projects/plugins文件夹中,但它没有被复制到platforms/ios/[appname]/plugins。
我有这个在我的config.xml:
<feature name="PushPlugin">
<param name="android-package" value="com.plugin.gcm.PushPlugin"/>
<param name="ios-package" value="PushPlugin"/>
</feature>
我使用以下JavaScript:
var pushNotification;
function onDeviceReady() {
alert('ready');
try {
pushNotification = window.plugins.pushNotification;
if (typeof device == 'undefined') {
alert('device undefined');
}
if (device.platform == 'android' || device.platform == 'Android' || device.platform == 'amazon-fireos') {
pushNotification.register(successHandler, errorHandler, {
"senderID": "[senderid]",
"ecb": "onNotification"
});
} else {
alert('ios!');
pushNotification.register(tokenHandler, errorHandler, {
"badge": "true",
"sound": "true",
"alert": "true",
"ecb": "onNotificationAPN"
});
}
} catch (err) {
txt = "There was an error on this page.nn";
txt += "Error description: " + err.message + "nn";
alert(txt);
}
}
function onNotificationAPN(e) {
// handles APNS notifications for iOS
if (e.alert) {
navigator.notification.alert(e.alert);
}
if (e.sound) {
var snd = new Media(e.sound);
snd.play();
}
if (e.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
}
}
function onNotification(e) {
//handles notifications for Android (left out)
}
function tokenHandler(result) {
alert('device token = ' + result);
}
function successHandler(result) {
alert('success = ' + result);
}
function errorHandler(error) {
alert('error = ' + error);
}
document.addEventListener('deviceready', onDeviceReady, false);
我确实看到了'ready'和'ios!,但在此之后,什么也没有发生。
问题解决了!结果我不得不复制Plugin文件并手动添加到xcode项目中。