Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send push notification using phonegap and parse

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.

like image 469
Struggling Developer Avatar asked Dec 06 '13 10:12

Struggling Developer


1 Answers

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

like image 199
Hugo Delsing Avatar answered Nov 15 '22 16:11

Hugo Delsing