gab*_*iel 5 push-notification phonegap-build pushwoosh
我正在尝试使用Pushwoosh推送通知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>
Run Code Online (Sandbox Code Playgroud)
这是我的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
});
}
};
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激...谢谢!
小智 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]));
}
);
}
Run Code Online (Sandbox Code Playgroud)
另外,您可能需要仔细检查 AndroidManifest.xml。不幸的是,很容易出错:((
| 归档时间: |
|
| 查看次数: |
1810 次 |
| 最近记录: |