I am building an iOS application with Cordova / Phonegap that uses push notifications.
I use PushPlugin to implement notifications within the client application.
I set the APNS side, and when I send a notification when my application is paused or closed, the notification displays correctly. However, when I am in the application, the plugin throws an error:
Can't find variable: onNotificationAPN
When I look at the output of Xcode, I see that the notification is actually delivered:
2014-03-02 23:12:58.746 EASP 2014[5792:60b] Notification received
2014-03-02 23:12:58.747 EASP 2014[5792:60b] Msg: {"alert":"testing...",foreground:"1"}
However, nothing happens in the application, and I'm stuck in the onNotificationAPN error.
I tried everything to debug this, but I was stuck. Any idea why this is happening?
Here is the code I use to set up notifications:
var pushNotification;
pushNotification = window.plugins.pushNotification;
if ( device.platform == 'android' || device.platform == 'Android' ) {
pushNotification.register(
successHandler,
errorHandler, {
"senderID":"<xxxxx>",
"ecb":"onNotificationGCM"
}
);
}
else {
pushNotification.register(
tokenHandler,
errorHandler, {
"badge":"false",
"sound":"false",
"alert":"true",
"ecb":"onNotificationAPN"
}
);
}
function successHandler (result) {
console.log('result = ' + result);
navigator.notification.alert(
result,
onConfirm,
'Title of app',
'Dismiss'
);
}
function errorHandler (error) {
console.log('error = ' + error);
}
function tokenHandler (result) {
var uuid = device.uuid;
var platform = device.platform;
console.log(platform);
if (platform == 'iOS'){
var os = 'ios';
} else {
var os = 'android';
}
hash = result+'<title of app>';
hash = md5(hash);
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
var url = 'https://<notification server>/?token='+result+'&id='+uuid+'&hash='+hash+'&os='+os;
xmlHttp.open( "GET", url, false );
xmlHttp.send( null );
console.log(xmlHttp.responseText);
return xmlHttp.responseText;
}
function onNotificationAPN (event) {
console.log(event);
if ( event.alert ) {
navigator.notification.alert(event.alert);
}
if ( event.sound ) {
var snd = new Media(event.sound);
snd.play();
}
if ( event.badge ) {
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
}
}
function receivedEvent(id) {
navigator.notification.alert(
id,
onConfirm,
'<title of app>',
'Dismiss'
);
}
function onConfirm(buttonIndex,id) {
}