android – How to send/receive push notifications from ionic mobile app?-ThrowExceptions

Exception or error:

I have a messaging app built using the Ionic framework (on cordova). I plan on building this for android, and I’d like a way to send and recieve push notifications from the app using javascript/ionic.

Are there any good tutorials out there on how to go about setting something like this up?

How to solve:

There is example application made available by Holly Schinsky. The core of it is the usage of PushPlugin which is the standard method to handle push notifications on Cordova. There is quite extensive tutorial provided for this subject on their documentation on that GitHub repository. The main method is pushNotification.register which registers the device to listen for push notifications.

If you instead need to trigger notification locally, you might want to take a look at Local notification plugin instead. With it you can add notifications to be shown on the device without the need for external services to send the push notifications.


Use this plugin

Android devices receive push notifications through the Google Cloud Messaging (GCM) service, whereas iOS devices receive them from the Apple Push Notifications (APN) Service.

The way the notifications are received (by sound, alert etc) is a combination of the options set in the application code upon registration as well as the user’s device settings for notifications.

If you want more specific follow below tutorial :


ngCordova has a plugin that supports Push Notifications. It has sample code for iOS and Android. Check it out:


The latest phonegap-plugin-push allows you to register and receive push notifications in your ionic apps. It is maintained at the following Github link:


cordova plugin add --variable SENDER_ID="XXXXXXX"

Where the XXXXXXX in SENDER_ID="XXXXXXX" maps to the project number in the Google Developer Console. To find the project number login to the Google Developer Console, select your project and click the menu item in the screen shot below to display your project number.


If you are not creating an Android application you can put in anything for this value.

Note: You may need to specify the SENDER_ID variable in your package.json.

"cordovaPlugins": [
      "variables": {
        "SENDER_ID": "XXXXXXX"
      "locator": "phonegap-plugin-push"

Note: You need to specify the SENDER_ID variable in your config.xml if you plan on installing/restoring plugins using the prepare method. The prepare method will skip installing the plugin otherwise.

<plugin name="phonegap-plugin-push" spec="1.6.0">
    <param name="SENDER_ID" value="XXXXXXX" />

After installation you can now add code below to your main javascript file to register and receive push notifications:

    $ionicPlatform.ready(function () {

         var push = PushNotification.init({
           android: {
             senderID: "XXXXXXX"//, //project token number (12 digit) from
             // forceShow: "true", //force show push notification when app is in foreground on Android only.
           browser: {
             pushServiceURL: ''
           ios: {
             /*senderID: "XXXXXXX",*/ //If using GCM for ios, project token number (12 digit) from
             /*gcmSandbox: 'true',*/ //If using GCM for ios
             alert: 'true',
             badge: 'true',
             sound: 'true',
           windows: {}

         PushNotification.hasPermission(function (permissionResult) {
           if (permissionResult.isEnabled) {
             $log.debug("has permission for push notification");

             /*Register device with GCM/APNs*/
             push.on('registration', function (data) {
               // data.registrationId
               $log.debug("data.registrationId: " + data.registrationId);          

             push.on('notification', function (data) {
               // data.message,
               // data.title,
               // data.count,
               // data.sound,
               // data.image,
               // data.additionalData

             push.on('error', function (e) {
               // e.message
               $log.debug("e.message: " + e.message);

Leave a Reply

Your email address will not be published. Required fields are marked *