简体   繁体   中英

PhoneGap Build Push Notification (Android)

I am having trouble receiving any type of callback for the push notifications plugin for phonegap build, I have included the plugin inside config.xml.

I have signed up to GCM and got my project number needed for pushNotification.register().

I also have access to the window.plugins.pushNotification object so I know it's included the plugin.

  • PhoneGap Build Version: 3.1
  • Hydration: Disabled
  • Debug: Enabled
  • Device: Samsung Tab 2

My index.html js files included are:

<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>
<script type="text/javascript" src="js/lib/jquery.js" ></script>
<script type="text/javascript" src="js/lib/handlebars.js"></script>
<script type="text/javascript" src="js/handlebars/helpers.js"></script>
<script type="text/javascript" src="js/plugins/fastclick.js"></script>
<script type="text/javascript" src="js/app.js"></script>

My config.xml plugins included are:

// plugins
<gap:plugin name="org.apache.cordova.console" />
<gap:plugin name="org.apache.cordova.device" />
<gap:plugin name="org.apache.cordova.geolocation" />
<gap:plugin name="org.apache.cordova.dialogs" />
<gap:plugin name="org.apache.cordova.inappbrowser" />
<gap:plugin name="org.apache.cordova.splashscreen" />
<gap:plugin name="com.phonegap.plugins.pushplugin" />
// access to external domains
<access origin="*"/>

My app.js call to pushNotification.register()

var app = {
init: function() {
    document.addEventListener("deviceready", this.onDeviceReady, false);
    },

    onDeviceReady: function(){
       // DO STUFF
       // ....

       // ENABLE PUSH
       this.push_init();
    },

    push_init: function(){
        app.SENDER_ID = 123456789; // replaced by my actual GCM project no

        var pushNotification = window.plugins.pushNotification;

        pushNotification.register( 
            function(){alert('Push: win');}, // never called
            function(){alert('Push: Error');},  // never called
            { senderID: app.SENDER_ID, ecb: "app.push_android" }
        );
    },
   // never called
   push_android: function(e){
       alert('connection established...');
   console.log( 'successfully started android' );
   console.log( e );
   }

};

// start the app
app.init();

After that is called nothing is executed, app.push_android() is a function of app object.

If i don't enter a senderID I get an error saying no sender ID so I know that something is working. This is so frustrating any ideas?

PS - I also noticed something weird, when I console.log the window.plugins.pushNotification it returns an empty object, however I can still call window.plugins.pushNotification.register(), but I thought I would be visible inside the console.log.

I think I've found the solution.

I was passing an integer instead of a string for the property senderID in the object

Doesnt work

pushNotification.register( 
    function(){alert('Push: win');}, // NOT called
    function(){alert('Push: Error');},  // NOT called
    { senderID: 123456789, ecb: "app.push_android" }
);

DOES work

pushNotification.register( 
    function(){alert('Push: win');}, // called
    function(){alert('Push: Error');},  // called
    { senderID: "123456789", ecb: "app.push_android" }
);

Try this push notification code -

var pushNotification;

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

function onDeviceReady() {

   try {
       pushNotification = window.plugins.pushNotification;
       if (device.platform == 'android' || device.platform == 'Android') {
           pushNotification.register(successHandler, errorHandler, { "senderID": "123456789", "ecb": "onNotificationGCM" });    // required!
          }
      else {
          pushNotification.register(tokenHandler, errorHandler, { "badge": "true", "sound": "true", "alert": "true", "ecb": "onNotificationAPN" }); // required!
          }
      }  
    catch (err) {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.message + "\n\n";
        alert(txt);
     }
   };

// handle GCM notifications for Android
function onNotificationGCM(e) {
    switch (e.event) {
        case 'registered':
            if (e.regid.length > 0) {
                alert(e.regid);
                storeToken(e.regid);
            }
            break;

        case 'message':
            if (e.foreground) {
                var my_media = new Media("beep.wav");
                my_media.play();
            }
            else {  
              // otherwise we were launched because the user touched a notification in the notification tray.
            }

            break;

       case 'error':
           break;
       default:
          break;
    }
}

Refer Link

Refer Devgirl's Weblog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM