2

I've tried to register my device using pushNotification, using a demo. It doesn't work. Print "Cordova PushNotification Plugin Demo" and "registering android" and look the alert "OK" (successHandler function works). The problem is that doesn't work the "ecb". Why? Can you help me?

The plugin that I've installed with Cordova are: PushPlugin, Device, Notification, InAppBrowser and Network Information.

I've tried the app on my LG L9 and on emulator. Same problem.

This is the code:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery_1.5.2.min.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>        
<script type="text/javascript">
var pushNotification;

document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available

function onDeviceReady() {
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android') {
    $("#app-status-ul").append("<li>registering android</li>");
    window.plugins.pushNotification.register(successHandler, errorHandler, {
        "senderID": "my_id",
        "ecb": "onNotificationGCM"
    }); // required!
} else {
    $("#app-status-ul").append("<li>registering iOS</li>");
    pushNotification.register(tokenHandler, errorHandler, {
        "badge": "true",
        "sound": "true",
        "alert": "true",
        "ecb": "onNotificationAPN"
    }); // required!
}
}

// handle APNS notifications for iOS

function onNotificationAPN(e) {
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);
}
}
// handle GCM notifications for Android

function onNotificationGCM(e) {
navigator.notification.alert(e.event);
switch (e.event) {
case 'registered':
    if (e.regid.length > 0) {
        navigator.notification.alert(e.regid);
        // Your GCM push server needs to know the regID before it can push to this device
        // here is where you might want to send it the regID for later use.
        $("#app-status-ul").append("<li>regID = " + e.regid +"</li>");
        sessionStorage.setItem("deviceId",e.regid);
    }
    break;
case 'message':
    // if this flag is set, this notification happened while we were in the foreground.
    // you might want to play a sound to get the user's attention, throw up a dialog, etc.
    if (e.foreground) {
        navigator.notification.alert('--INLINE NOTIFICATION--');
        // if the notification contains a soundname, play it.
        var my_media = new Media("/android_asset/www/" + e.soundname);
        my_media.play();
    } else { // otherwise we were launched because the user touched a notification in the notification tray.
        if (e.coldstart) navigator.notification.alert('--COLDSTART NOTIFICATION--');
        else navigator.notification.alert('--BACKGROUND NOTIFICATION--');
    }
    navigator.notification.alert(e.payload.message);
    navigator.notification.alert('MESSAGE -> MSGCNT: ' + e.payload.msgcnt);
    break;
case 'error':
    navigator.notification.alert('ERROR -> MSG:' + e.msg);
    break;
default:
    navigator.notification.alert('EVENT -> Unknown, an event was received and we do not know what it is');
    break;
}
}

function tokenHandler(result) {
navigator.notification.alert(result, null, 'Alert', 'OK');
sessionStorage.setItem("deviceId", result);
sessionStorage.setItem("notificationServer", "APNS");
// 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.
}

function successHandler(result) {
navigator.notification.alert(result, null, 'Alert', 'OK');
sessionStorage.setItem("deviceId", result);
sessionStorage.setItem("notificationServer", "GCM");
}

function errorHandler(error) {
navigator.notification.alert(error, null, 'Alert', 'OK');
}

        document.addEventListener('deviceready', onDeviceReady, true);

     </script>
    <div id="home">
        <div id="app-status-div">
            <ul id="app-status-ul">
                <li>Cordova PushNotification Plugin Demo</li>
            </ul>
        </div>
    </div>
Simone Sessa
  • 795
  • 1
  • 12
  • 35
  • onNotificationGCM must have scope in window. See logcat to show more error – Hanh Le Aug 21 '14 at 01:26
  • That is? I don't understand, can you give an example? – Simone Sessa Aug 21 '14 at 11:38
  • What platform and device you run plugin? Note: PushPlugin not working in IOS emulator, you must test on real device. In Android, you can test it in emulator with google API target(included google service for GCM working) – Hanh Le Aug 22 '14 at 01:14

3 Answers3

6

As Hanh Le said, you need to have the ecb-callback to be accessible from the window-object

Like this

    pushNotification.register(tokenHandler, errorHandler, {
       "badge": "true",
       "sound": "true",
       "alert": "true",
       "ecb": "window.onNotificationAPN"
    }); // required!

and then replace

     function onNotificationAPN(e) {

with

    window.onNotificationAPN = function(e) {

EDIT: here is the whole code you posted edited in a way I think should work:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery_1.5.2.min.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>        
<script type="text/javascript">
var pushNotification;

document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available

function onDeviceReady() {
    pushNotification = window.plugins.pushNotification;
    if (device.platform == 'android' || device.platform == 'Android') {
        $("#app-status-ul").append("<li>registering android</li>");
        pushNotification.register(successHandler, errorHandler, {
           "senderID": "my_id",
           "ecb": "window.onNotificationGCM"
        }); // required!
    } else {
         $("#app-status-ul").append("<li>registering iOS</li>");
         pushNotification.register(tokenHandler, errorHandler, {
            "badge": "true",
            "sound": "true",
            "alert": "true",
            "ecb": "window.onNotificationAPN"
         }); // required!
   }
}

// handle APNS notifications for iOS

window.onNotificationAPN = function(e) {
   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);
   }
}
// handle GCM notifications for Android

window.onNotificationGCM = function(e) {
   navigator.notification.alert(e.event);
   switch (e.event) {
      case 'registered':
      if (e.regid.length > 0) {
          navigator.notification.alert(e.regid);
          // Your GCM push server needs to know the regID before it can push to this    device
         // here is where you might want to send it the regID for later use.
         $("#app-status-ul").append("<li>regID = " + e.regid +"</li>");
         sessionStorage.setItem("deviceId",e.regid);
     }
     break;
     case 'message':
        // if this flag is set, this notification happened while we were in the foreground.
        // you might want to play a sound to get the user's attention, throw up a dialog, etc.
         if (e.foreground) {
            navigator.notification.alert('--INLINE NOTIFICATION--');
            // if the notification contains a soundname, play it.
            var my_media = new Media("/android_asset/www/" + e.soundname);
            my_media.play();
         } else { // otherwise we were launched because the user touched a notification in the notification tray.
           if (e.coldstart) navigator.notification.alert('--COLDSTART NOTIFICATION--');
           else navigator.notification.alert('--BACKGROUND NOTIFICATION--');
         }
         navigator.notification.alert(e.payload.message);
         navigator.notification.alert('MESSAGE -> MSGCNT: ' + e.payload.msgcnt);
         break;
    case 'error':
       navigator.notification.alert('ERROR -> MSG:' + e.msg);
    break;
    default:
       navigator.notification.alert('EVENT -> Unknown, an event was received and we do not know what it is');
    break;
  }
}

function tokenHandler(result) {
   navigator.notification.alert(result, null, 'Alert', 'OK');
   sessionStorage.setItem("deviceId", result);
   sessionStorage.setItem("notificationServer", "APNS");
   // 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.
}

function successHandler(result) {
   navigator.notification.alert(result, null, 'Alert', 'OK');
   sessionStorage.setItem("deviceId", result);
   sessionStorage.setItem("notificationServer", "GCM");
}

function errorHandler(error) {
   navigator.notification.alert(error, null, 'Alert', 'OK');
}

document.addEventListener('deviceready', onDeviceReady, true);

</script>
<div id="home">
    <div id="app-status-div">
        <ul id="app-status-ul">
            <li>Cordova PushNotification Plugin Demo</li>
        </ul>
    </div>
</div>
Joonas Ahola
  • 199
  • 5
  • And for Android? I've tried this: `window.onNotificationGCM = function(e) {` - - - Then i've edited this `window.plugins.pushNotification.register(successHandler, errorHandler, { "senderID": "my_id", "ecb": "window.onNotificationGCM"});` and again in this `plugins.pushNotification.register` and again with this `pushNotification.register`, but they doesn't work.. – Simone Sessa Aug 21 '14 at 20:12
  • I've tried also with other combinations, but nothing. I'll wait for your advice. – Simone Sessa Aug 21 '14 at 20:41
  • on iOS, testing the notification can't be done in the simulator, but on your android device it should work. How are you sending the notifications? and if your device registers correctly, can you see the incoming notifications in logcat? – Joonas Ahola Aug 22 '14 at 05:53
  • Maybe I'm wrong.. But with the function "onNotificationGCM" he should print (using `$("#app-status-ul").append("
  • regID = " + e.regid +"
  • ")`) the registration ID? Now, I'm writing with tablet and I can't make changes to the code. In logcat I don't see nothing about "regID". But, for Android, what is the right method? I will do further testing after the 13. – Simone Sessa Aug 22 '14 at 06:49
  • In locgat I don't see nothing about "regID" beacuase I don't "print" the id in a log, but in a "ul" just with the `$("#app-status-ul").append(...` – Simone Sessa Aug 22 '14 at 06:59
  • Sorry I misunderstood, I thought your device registered corerctly but didn't receive the notifications. I've got this plugin working on iOS but have never tried it on android, so I'm not exactly sure what is wrong here. The code looks good to go and if the successHandler-function gets called it implies something did happen correctly. I'll edit my original answer for some clarity – Joonas Ahola Aug 22 '14 at 08:00
  • Thanks for your reply and your patience. I used your code, changed the id and added this `if (e.regid.length > 0) {code .. navigator.notification.alert(error, null, 'Alert', 'yes');} else navigator.notification.alert(error, null, 'Alert', 'no');` to print at least an alert, but it doesn't work. I don't understand much, I'm still learning, but the problem couldn't be in the PushNotification.js file? Is this: https://github.com/phonegap-build/PushPlugin/blob/master/Example/www/PushNotification.js - PS: I've used this guide at first: https://github.com/phonegap-build/PushPlugin – Simone Sessa Aug 22 '14 at 13:09
  • After I try to study this plugin, but if you help me, I'd be grateful – Simone Sessa Aug 23 '14 at 11:45
  • Nothing. I searched on Google and I've tried other guide, but it doesn't work. – Simone Sessa Aug 23 '14 at 13:57
  • Up! Help me please :( – Simone Sessa Aug 27 '14 at 13:21
  • I'm trying to but it's a bit hard, I see ne obvious faults in the code I posted, I tried the code on iOS and it works. Have you tried to copy the whole example project on [github](https://github.com/phonegap-build/PushPlugin/tree/master/Example/www) and see if that works? – Joonas Ahola Aug 28 '14 at 11:17
  • I've download this example: https://github.com/phonegap-build/PushPlugin then I've replaced "phonegapp.js" with "cordova.js". The app doesn't print the id, it prints only "- Cordova PushNotification Plugin Demo - deviceready event received - registering Android - success:OK".. – Simone Sessa Aug 30 '14 at 11:18