how to send push notification using phonegap and parse

13,059

I followed this tutorial which worked very well directly. It also explains how to get the device token.

It is alerted for you to type it over, but you can also hook your phone up to your computer and read the logcat files. (You can use the "monitor" tool in the android SDK)

UPDATE WITH EXAMPLE

Most steps are basically a direct copy of devgirls tutorial I mentioned before

In windows command prompt:

  1. phonegap create quickpush
  2. cd quickpush
  3. phonegap local build android
  4. phonegap local plugin add https://github.com/phonegap-build/PushPlugin

  5. I skipped this, I dont copy the file to the www dir. I just leave it where it is.

  6. add <script type="text/javascript" src="PushNotification.js"></script> to index.html

  7. add <gap:plugin name="com.phonegap.plugins.pushplugin" /> to config.xml (this is different from site and solves not supported error)

  8. Copy the push code in the onDeviceReady function in /js/index.js file. Obviously add your own key from google

    alert('device ready');
    try {
        var pushNotification = window.plugins.pushNotification;
        pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"--SENDER ID FROM GOOGLE--","ecb":"app.onNotificationGCM"});
    } catch (ex) {
        alert('error: ' + ex);
    }
    
  9. Copy the callback handler function in /js/index.js file

    successHandler: function(result) {
        alert('Callback Success! Result = '+result)
    },
    errorHandler:function(error) {
        alert(error);
    },
    onNotificationGCM: function(e) {
        switch( e.event )
        {
            case 'registered':
                if ( e.regid.length > 0 )
                {
                    console.log("Regid " + e.regid);
                    alert('registration id = '+e.regid);
                }
            break;
    
            case 'message':
              // this is the actual push notification. its format depends on the data     model from the push server
              alert('message = '+e.message+' msgcnt = '+e.msgcnt);
            break;
    
            case 'error':
              alert('GCM error = '+e.msg);
            break;
    
            default:
              alert('An unknown GCM event has occurred');
              break;
        }
    }
    
  10. build the app: phonegap remote build android

Share:
13,059
Struggling Developer
Author by

Struggling Developer

Updated on July 24, 2022

Comments

  • Struggling Developer
    Struggling Developer almost 2 years

    I am creating an android app using php,jquery and phonegap. I have searched so many things in google but i cant find to send push notification. I have seen this Phonegap and Parse.com Push Notifications IOS But i am not clear ho can i get deviceToken.

    I have also seen the below

    https://parse.com/questions/php-rest-example-of-targeted-push

    I understood how to send notification. But without devicetoken how can i send push notification. Can anybosy tell me how can i get the device token.

  • Struggling Developer
    Struggling Developer over 10 years
    i am following the tutorial on part1>4 step it showing error "error fetching problem make sure it is accessible on your path" could you tell me why it is showing
  • Hugo Delsing
    Hugo Delsing over 10 years
    That seems like half of an error. Something that is similar to that message is when you dont have GIT installed, or dont have GIT added to your path variable. Perhaps blog.countableset.ch/2012/06/07/adding-git-to-windows-7-path
  • Hugo Delsing
    Hugo Delsing over 10 years
    you need it to download the plugin. To locally compile/debug my apps I have installed android SDK, ANT and GIT. Saves a lot of time with publishing and then scanning the QR code :)
  • Struggling Developer
    Struggling Developer over 10 years
    at last i have created a sample app and tried to upload in phonegap but it is hsowing error "plugin unsupported: com.adobe.plugins.pushplugin " @Hugo Delsing could you please tell me why it is showing
  • Hugo Delsing
    Hugo Delsing over 10 years
    I honestly have no idea why it would show that. Some guesses: You are using an older version of phonegap. You installed the plugin in another dir on your computer and not included in the project. You need to compile it localy. I'm fairly new at phonegap myself. The tutorial worked for me, but I havent encountered many strange errors yet.
  • Hugo Delsing
    Hugo Delsing over 10 years
    Just checked and indeed it builds localy but not remotely. First hit on google with the error solved it: community.phonegap.com/nitobi/topics/…
  • Struggling Developer
    Struggling Developer over 10 years
    Hey @Hugo Delsing sorry for asking. Could you give me a sample copy of this app ...
  • Hugo Delsing
    Hugo Delsing over 10 years
    I'll see if I can build something tomorrow. Current version contains production code.
  • Hugo Delsing
    Hugo Delsing over 10 years
    Added example to solution
  • SMUsamaShah
    SMUsamaShah almost 10 years
    Thanks for simplified tutorial
  • Hugo Delsing
    Hugo Delsing over 8 years
    The class not found error seems to be related to wrong config files. See Class not found. On devgirls site, somebody mentioned the same problem and the solution was: 'I know this is a bit late, but I was able to fix the “Class not found” error by rebuilding the app without Hydration from inside of PhoneGap Build.'