我正在尝试使用Pushwoosh Push Notifications phonegap构建插件,但registerDevice回调永远不会在我的Android设备上触发,但是initPushwoosh函数会触发,因为我在代码中看到来自警报(“initPushwoosh”)的警报以下
下面是我的config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- config.xml reference: https://build.phonegap.com/docs/config-xml -->
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "com.phonegap.hello-world"
version = "1.0.0">
<name>Push notification</name>
<description>
Camera example app.
</description>
<author href="http://phonegap.com" email="support@phonegap.com">
PhoneGap Team
</author>
<preference name="phonegap-version" value="3.3.0" />
<gap:plugin name="org.apache.cordova.core.camera" />
<plugin name="PushNotification"
value="com.pushwoosh.plugin.pushnotifications.PushNotifications" onload="true"/>
<access origin="*.pushwoosh.com" />
</widget>
这是我的index.js,我只是将项目ID和appid更改为XXXX,这样我就不会错误地透露过多。
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 explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
alert("onDeviceReady" );
app.initPushwoosh();
app.receivedEvent('deviceready');
},
initPushwoosh: function() {
alert("initPushwoosh" );
var pushNotification = window.plugins.pushNotification;
pushNotification.registerDevice(
{ projectid: "XXX-XXX-XXXX 3", appid : "XXXXX-XXXXX" },
function(status) {
var pushToken = status;
alert('push token: ' + pushToken);
},
function(status) {
alert(JSON.stringify(['failed to register ', status]));
});
document.addEventListener('push-notification', function(event) {
var title = event.notification.title;
var userData = event.notification.userdata;
if (typeof(userData) != "undefined") {
alert('user data: ' + JSON.stringify(userData));
}
navigator.notification.alert(title);
});
},
// 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);
},
takePicture: function() {
navigator.camera.getPicture( function( imageURI ) {
alert( imageURI );
},
function( message ) {
alert( message );
},
{
quality: 50,
destinationType: Camera.DestinationType.FILE_URI
});
}
};
任何帮助都将非常感谢...谢谢!
答案 0 :(得分:1)
Cordova已使用函数cordova.require()替换了window.plugins。您需要查找定义插件的命名空间。对于pushwoosh来说:&#34; com.pushwoosh.plugins.pushwoosh.PushNotification&#34;
所以而不是:
var PushNotification = window.plugins.PushNotification;
试试这个:
var PushNotification = cordova.require("com.pushwoosh.plugins.pushwoosh.PushNotification");
答案 1 :(得分:0)
您使用的是最新版本的插件吗?如果是这样,请查看最新指南: http://www.pushwoosh.com/programming-push-notification/android/android-additional-platforms/phonegapcordova-sdk-integration/
最近已更改,initPushwoosh功能如下所示:
function initPushwoosh()
{
var pushNotification = window.plugins.pushNotification;
//set push notifications handler
document.addEventListener('push-notification', function(event) {
var title = event.notification.title;
var userData = event.notification.userdata;
if(typeof(userData) != "undefined") {
console.warn('user data: ' + JSON.stringify(userData));
}
navigator.notification.alert(title);
});
//initialize Pushwoosh with projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID". This will trigger all pending push notifications on start.
pushNotification.onDeviceReady({ projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID" });
//register for pushes
pushNotification.registerDevice(
function(status) {
var pushToken = status;
console.warn('push token: ' + pushToken);
},
function(status) {
console.warn(JSON.stringify(['failed to register ', status]));
}
);
}
另外,您可能需要仔细检查AndroidManifest.xml。不幸的是,它很容易弄错:((