2

I have an android app built on apache cordova. I want push notifications for this application. I have used Visual studio 2015 IDE to built this application. I have already installed some plugins for this application like cordova-plugin-device-orientation, statusbar etc. To enable push notifications for this app I have installed phonegap-plugin-push (version 2.1.0) and provided the sender Id for my application which I created using Firebase. By creating new project, added android app, downloaded google-services.json file and placed it in my project.

<script>
        function setupPush() {
        //This line below throws error on simulator saying PushNotification //is not defined
            var push = PushNotification.init({
                "android": {
                    "senderID": "XXXXXXXXXX"
                },
                "ios": {
                    "sound": true,
                    "alert": true,
                    "badge": true
                },
                "windows": {}
            });
            alert('registered');
            push.on('registration', function (data) {
                alert('registration event' + data.registrationId);
                console.log("registration event: " + data.registrationId);
                var oldRegId = localStorage.getItem('registrationId');
                if (oldRegId !== data.registrationId) {
                    // Save new registration ID
                    alert('registration event' + data.registrationId)
                    localStorage.setItem('registrationId', data.registrationId);
                    // Post registrationId to your app server as the value has changed
                }
            });

            push.on('error', function (e) {
                console.log("push error = " + e.message);
            });
        }
       
        $(document).on('deviceready', function deviceIsReady() {
            console.log('Device is ready!');
            setupPush();
        });
    </script>

I found this code here.

Warda
  • 108
  • 3
  • 10

1 Answers1

0

What is your cordova CLI and cordova platform android version? I am using 6.2.3 android version and it run normally.

Please try to install the plugin first, after all the plugin install success than install the android platform for project.

Yuk_dev
  • 318
  • 1
  • 6
  • 16
  • cordova-cli: 6.1.1, "android": "5.1.1". My android app is already deployed on play store so the android platform is already installed can't change it now. Just want to add push notification service for my android app. – Warda Nov 17 '17 at 07:05
  • @Warda Your plugin version is not allow for 5.1.1, you must using 6.2.1 or higher, please take a reference in https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/INSTALLATION.md – Yuk_dev Nov 17 '17 at 09:14
  • have you build your apache cordova android app project in visual studio 2015? – Warda Nov 17 '17 at 09:29