Jyoshna
Jyoshna

Reputation: 253

How to implement push notifications in Cordova Platform?

I am trying to implement the push notifications concept in my app. After successful installation of plugin, push.on(registration) method is not calling

My Project structure is projectname/platforms/android/assets/www

In www folder contains all html,js,css files

notification.js file, I have called in homepage.html

I have written the code in notification.js is:

document.addEventListener('deviceready', pushNotification, false);
function PushNotification(){
var push = PushNotification.init({ "android": {"senderID": "GCMProjectId(123456789)"},"ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );
       push.on('registration', function(data) {
            alert("registration id is:"+data.registrationId);
            var id = localStorage.getItem("userId");
            var notifyInput = {
                        "token":data.registrationId,
                        "type":"android",
                        "uid":id
               }
            });

    push.on('notification', function(data) {
              alert(data.message);
            });

    push.on('error', function(e) {
            // e.message
            alert("error function calling on push notifications");

            });

}

Here, push.on(registration) and push.on(notification) the method is not calling please let us know the possible ways to get the notification for a specific device

Upvotes: 1

Views: 3093

Answers (1)

Jyoshna
Jyoshna

Reputation: 253

After successful installation of push notification plugin, i have received the notification in Android

Process:

By using the below link i have installed the push-notification plugin


cordova plugin add https://github.com/phonegap/phonegap-plugin-push --variable SENDER_ID="xxxxxxxxxxxxxx"

Installation Requirements:

-Android version > 6.0.0
-iOS Version > 4.3.0 better

For iOS Version, pods required.so we need to install pod

sudo gem install cocoapods

For GCM Registration: https://developers.google.com/mobile/add

After succesfull installation, pod file will be created . Once it done the open the project.xcworkspace file. Then ios app will run properly

If you are calling the notification in middle of the app, then write the addEventListener method

document.addEventListener('deviceready', pushNotification, false);

    function PushNotification(){
    var push = PushNotification.init({ "android": {"senderID": "xxxxxx(refers project number in GCM)"},"ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );

           push.on('registration', function(data) {
                alert("registration id is:"+data.registrationId);
               // registration id need to pass your notification server

                });

        push.on('notification', function(data) {
                  alert(data.message);
                  // you receive the notification
                });

        push.on('error', function(e) {
                // e.message
                alert("error function calling on push notifications");

                });

    }

Upvotes: 2

Related Questions