Open webaffin opened 9 years ago
Hello Robert,
Based on the log in the console it appears that the event is correctly received, but perhaps there is no registered handler. In this case the app should execute the logic in "onNotificatioAPN".
What I'd suggest here is to make sure that you are calling the register
method of the plugin when the app starts. Thus you will be sure that the JS callback is supplied to the native code. For example - the app is coldstarted through a notification - call register
, for example, in the device ready event handler, and the logic in "onNotificatioAPN" should be executed immediately.
Another check I'd suggest is to verify that "onNotificatioAPN" is available in this scope.
Hi Anton,
thank you for your suggestions.
I think the plugin is correctly registered, because I already get the device token from function tokenHandler
(and send it to a server).
I tried the provided example index.html
, which should work, I assume. But same issue here. No popup/alert when app is in foreground.
For testing purposes I cleared the whole content of onNotificationAPN
to only make a console output "I'm in" - but this output never gets displayed.
Do you have any further ideas?
Thanks in advance, Robert
Found another thread with similar/same issue: https://github.com/phonegap-build/PushPlugin/issues/68 Not solved yet.
Any further ideas?
I'm still having the problem described before:
Initialization of push works, because I get the device token. Sending push messages to device with given token is successful, when App is in background (sound, badge, alert etc.). But when App is in foreground nothing happens, only the log output in xcode:
`2015-04-27 14:45:05.395 XYZ[3188:413524] Msg: {"alert":"Hello World","badge":"2","sound":"assets/beep.wav",foreground:"1"}``
But no output from what I wrote in onNotificationAPN()
: "INSIDE: onNotificationAPN()"
And no alert/notification on device from navigator.notification.alert(e.alert, null, "Hinweis", "Ok");
(needed plugin is installed and successfully tested).
Here is my complete code, being included in index.html. Did I make something wrong?
I'm using Phonegap 4.2.0-0.26.0.
var app = {
base_url: 'http://app.xyz.de/',
pushNotification: null,
// 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 () {
console.log("device is ready");
StatusBar.overlaysWebView(false);
document.addEventListener("offline", app.onOffline, false);
document.addEventListener("online", app.onOnline, false);
app.init();
},
init: function () {
$('#network-error').hide();
$('#loading-page').show();
// connected: open web app
if (this.checkConnection() === true) {
console.log("connection is given");
app.initPushNotification();
}
// not connected: display local info page
else {
$('#loading-page').hide();
$('#network-error').show();
}
},
initJqm: function () {
$.mobile.pageLoadErrorMessage = 'Fehler beim Seitenaufruf';
// needed for phongegap
$.mobile.allowCrossDomainPages = true;
$.support.cors = true;
$.mobile.ajaxEnabled = false;
},
onOffline: function () {
$('#loading-page').hide();
$('#network-error').show();
},
onOnline: function () {
app.init();
},
callWebApp: function (token) {
console.log("in callWebApp()");
var url = app.base_url + 'index.html?token=' + token;
console.log("URL: " + url);
window.open(url, '_self', 'location=no');
},
checkConnection: function () {
var networkState = navigator.connection.type;
if (networkState == Connection.NONE) {
return false;
}
return true;
},
/********/
/* PUSH */
/********/
initPushNotification: function () {
console.log("Inside: initPushNotification");
try {
console.log(window.plugins);
app.pushNotification = window.plugins.pushNotification;
console.log(app.pushNotification);
console.log("After: window.plugins.pushNotification");
app.pushNotification.register(app.tokenHandler, app.errorHandler, {"badge": "true", "sound": "true", "alert": "true", "ecb": "app.onNotificationAPN"});
console.log("After: pushNotification.register");
}
catch(err)
{
var txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
console.log(txt);
}
},
// handle APNS notifications for iOS
onNotificationAPN: function (e) {
console.log("INSIDE: onNotificationAPN()");
if (e.alert) {
// showing an alert also requires the org.apache.cordova.dialogs plugin
console.log("Alert: " + e.alert);
navigator.notification.alert(e.alert, null, "Hinweis", "Ok");
}
//if (e.sound) {
// console.log("Sound" + e.sound);
// // playing a sound also requires the org.apache.cordova.media plugin
// var snd = new Media(e.sound);
// snd.play();
//}
//
//if (e.badge) {
// console.log("Badge: " + e.badge);
// pushNotification.setApplicationIconBadgeNumber(app.successHandler, e.badge);
//}
},
tokenHandler: function (result) {
console.log("Inside: tokenHandler");
console.log("Token: " + result);
app.callWebApp(result);
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send it the token for later use.
},
successHandler: function (result) {
console.log("Success: " + result);
},
errorHandler: function (error) {
console.log("Error: " + error);
app.callWebApp(0);
}
};
Hey Robert,
What I am assuming here is that app.onNotificationAPN is causing some scope issues in the JS. May I suggest that you place it in the global scope, something like:
function onNotificationAPN () {
// add body to the function
}
and register the device with the following code:
app.pushNotification.register(app.tokenHandler, app.errorHandler, {"badge": "true", "sound": "true", "alert": "true", "ecb": "onNotificationAPN"});
I know that you are not using Telerik Backend Services as an intermediary server, but this article from their documentation might be of any help to you - http://docs.telerik.com/platform/backend-services/development/javascript-sdk/push-notifications/handle-hybrid-push.
In regard to the issue you referenced, I am assuming it does not correlate exactly to the issue discussed here. However, there are some answers that might be helpful, and refer to the proposed fix above.
Let me know how this goes.
Hello Anton,
thank you very much for trying to help me and sorry, that I respond so late. I was kind of busy these days.
I tried outsourcing the function onNotificationAPN
before and it didn't work, too. I tried it once again now, precisely like you suggested. Same effect.
But then ... I had a problem with the allowed URLs in Phonegap's InApp-Browser (the app is just a website with phongap as native wrapper), so the URL opened not in-app but in normal Safari browser. When I got back to my app I only saw the start screen of the app. Now I sent a push notification to the app (still being in foreground) and the callback was actually fired! I corrected the issue with the allowed URLs, restarted the app. Now the URL was correctly opened with In-App-Browser again. I again sent a push notification, and ... nothing.
It seems that it has something to do with Phonegap's In-App-Browser in Foreground. But why that? I open it like this: window.open(url, '_self', 'location=no');
Strange problem.
Any suggestions?
Thanks in advance :)
Try making your ecb callbacks global for ios and android. i.e window.onNotificationAPN = function(){ ... } window.onNotification = function(){ ... }
Same issue here, only on iOS. In my case onNotificationAPN is not fired neither in foreground, nor in background. It is just not fired at all on iOS. I use ngCordova and it registers "ecb" parameter as "angular.element(" + injector + ").injector().get('$cordovaPush').onNotification". I thought it could be the issue, but exactly the same code perfectly works on Android.
Same isue with me. Any suggestion to fix this?
Same issue here. Push is fired in background but not in foreground. The code inside onNotificationAPN is never fired.
When app is in foreground and I send a notification, then I did not get an alert in app.
I just see in Xcode log that the notification is somehow recognized: 2015-04-27 14:45:05.392 XYZ[3188:413524] didReceiveNotification 2015-04-27 14:45:05.393 XYZ[3188:413524] Notification received 2015-04-27 14:45:05.395 XYZ[3188:413524] Msg: {"alert":"Hello World","badge":"2","sound":"assets/beep.wav",foreground:"1"}
But nothing else happens. No alert popup in app or something.
Seems that Javascript function "onNotificationAPN" is not called.
Any ideas?
Thanks, Robert
Xcode 6.4 / iOS 8.3 / iPhone 5S