In this article I’ll explain how to setup push notifications with PhoneGap and Android. I also wrote an article in how to setup push notifications with PhoneGap for iOS.

android

Prepare your PhoneGap app

First off, you have to install the Push Plugin for PhoneGap:

You also need the PhoneGap device plugin:

The next thing you need to do, is copy the PushNotification.js -file to the www-folder of your app (or in a subdirectory in your app, whatever, your app just needs this file). Include this file in your apps’ index.html -file:

Now we just need to add some JavaScript to allow our app to register the users’ device and to receive push notifications on it:

Please note that there is one minor difference between the Android and the iOS-approach: whereas iOS gives the registration ID in the pushSuccessHandler , Android sends a notification of the registered -event.

Tip: console.log() on Android

If you develop with PhoneGap you sometimes want to have access to the  console.log() -messages which derive from the Android device your testing on. By entering the following command in your terminal you can read these messages:

Setup your app within Google Developer Console

This is the part where we register our app with Google’s Cloud Messaging for Android-API (GCM), so our app is allowed to send push notifications by Google. I’ll try to explain step-by-step on how to set this all up:

  • Log in on the Google Developer Console.
  • Create a new API Project.
  • In the menu, select ‘API’s & Auth’.
  • Select ‘Google Cloud Messaging for Android’ en set it ‘ON’.
  • Then in the menu, select ‘Credentials’.
  • At ‘public key access’, create a new public key.
  • Select ‘server’-key and add the IP address(es) which is/are allowed to send push notifications. For test purposes you can add the address 0.0.0.0/0 .
  • Copy the API key that’s generated here. You need this in your PHP script to connect to the GCM server.
  • Log in on the Google Play Developer Console.
  • Select your app and go to ‘Services & Api’s’.
  • Bind your GCM App ID with your App. Your GCM App ID is the Project Number that’s assigned to your Google API Project.

That’s it! You now have set you app up at Google to send push notifications!

Setup your PHP Server

This example provides a simple example on how to setup a simple PHP server to send your push notifications to Google’s GCM server. There are out-of-the-box solutions for this, but it’s also good to have a bare minimum example on how to achieve this:

That’s it! Now you are able to send push notifications from your PHP server to your Android PhoneGap app.

Special thanks go out to Gonzalo Ayuso who wrote an excellent article on how to setup a PHP server to send push notifications to Android. I also wrote a dutch translation of this article.

Rating

Visitors give this article an average rating of 5.0 out of 5.

Was this post helpful for you?

11 thoughts on “Push notifications with PhoneGap and Android”

  1. marcobax22 says:

    Great article Giel! Thank you for sharing

  2. Anoop says:

    Hi,
    I am trying to implement push notification in Android-Phonegap. I am successfully getting following response – 1. deviceready event received 2. registering Android

    but i am not getting Google Reg. ID. I am following the following source code https://github.com/phonegap-build/PushPlugin

    Please let me know if i am missing something. Thanks in Advance.

    1. Giel Berkers says:

      This could have various reasons. Did you for example make sure to:

      • Setup your project in Google Developer console?
      • Add 0.0.0.0/0 as server for test purposes?
      • Bind your GCM app ID with your app in the Google Play console?

      Another ‘gotcha’ here might be that iOS gives you the registration ID on the successHandler, but GCM gives it in the notification handler:

  3. Talha Qamar says:

    Hi Giel i have setup my php server,I dont know where to mentioned this url in the javascript code.Please help me.

    1. Giel Berkers says:

      You don’t have to setup a URL in your JavaScript code; the Phonegap plugin handles the requests. Your PHP server makes a request to the GCM server and the GCM server communicates with your app.

  4. Hey thanks for this, i need more clarification on where to put the url. Where exactly in the phonegap plugiin.

  5. Rajashree says:

    Hi Giel can we setup a java server for this. Please let me know

    1. Giel Berkers says:

      Of course you can, any server can do. You’ll just have to write the Java equivalent to the code above.

  6. scibilo says:

    hi,
    everything works perfectly, but when I click on the notification and the app is in the background, in the DOM shows only the last notification.
    Collpasse_key is not set

    How to receive all messages in order of arrival?

    Thank you in advance

  7. Sam says:

    hi,
    can you please tell how to redirect to specific page/state/url when the user taps on that push notification!

  8. Alex says:

    hi,

    i´m getting ClassNotFound-Error (guess the PushPlugin).. i did exactly as you described – added the plugin and the javascript, customized the project id from the google developer console)
    what am i doing wrong? :(

Leave a Reply