0

im workin on phonegap v5.3.6 and cordova v5.3.3. I did everything on README but plugin is not working.

Here is my code below;

onDeviceReady: function() {
        var push = PushNotification.init({
            "android": {
                "senderID": "MY_SENDER_ID"
            },
            "ios": {},
            "windows": {}
        });
        push.on('registration', function(data) {
            console.log("registration event");
            document.getElementById("regId").innerHTML = data.registrationId;
            console.log(JSON.stringify(data));
        });

        push.on('notification', function(data) {
            console.log("notification event");
            console.log(JSON.stringify(data));
            var cards = document.getElementById("cards");
            var push = '<div class="row">' +
                  '<div class="col s12 m6">' +
                  '  <div class="card darken-1">' +
                  '    <div class="card-content black-text">' +
                  '      <span class="card-title black-text">' + data.title + '</span>' +
                  '      <p>' + data.message + '</p>' +
                  '    </div>' +
                  '  </div>' +
                  ' </div>' +
                  '</div>';
            cards.innerHTML += push;
        });

        push.on('error', function(e) {
            console.log("push error");
        });
    }

It's not throwing any success or error message. What is wrong on this code phrase?

Here is git repo of plugin : https://github.com/phonegap/phonegap-plugin-push

Thank you for your help

t13n
  • 317
  • 1
  • 13

2 Answers2

1

Try to use with Push Notification link. It is working fine with me.

Firstly register your device with,

pushNotification.register(
successHandler,
errorHandler,
{
    "senderID":"replace_with_sender_id", //It should be your project id that you will get from Google Developer Console while registering the project with package name.
    "ecb":"onNotification"
}

and add two events - success and failure like this,

function successHandler (result) {
     alert('result = ' + result); //Here you will get your device id.
}


function errorHandler (error) {
     alert('error = ' + error);
}

Also, add onNotification event, that will fire when device will received notification.

function onNotification(e){
     alert(e.event);
}
Dhruv
  • 1,801
  • 1
  • 15
  • 27
  • [Push Plugin](https://github.com/phonegap-build/PushPlugin) is deprecated, so i'm using [this one](https://github.com/phonegap/phonegap-plugin-push). Also other one did not work for me. – t13n Oct 21 '15 at 08:33
  • You may having some another issue. I have used https://github.com/phonegap-build/PushPlugin. Please put your error in the comment. – Dhruv Oct 23 '15 at 06:20
  • I try to figured out what happens but now it throws an error which is "Class not foud.". I search a fix for it and i tried all suggestions but i failed. I think these plugins did not work because of config.xml file. I'll inform you after i fix config.xml file. Thank you btw. – t13n Oct 23 '15 at 08:32
  • Hi @DHruv , i tried to do something then i succeed but there is some problems again. i sent infos to register to gcm bu i did not take a callback and i dont know why. – t13n Nov 23 '15 at 11:37
1

For people finding an answer the best Push Notification Plugin for Phonegap existing currently is the one mentioned here: Phonegap Push Notification Plugin. It is working fine on all Android and iOS versions. It can be used as below:

var push = PushNotification.init({
            android: {
                senderID: "XXXXXXXXXXXX",
            },
            ios: {
                alert: "true",
                badge: "true",
                sound: "true",
            }
        });
push.on('registration', function(data) {
    console.log(data.registrationId);
    registerDeviceToken(data.registrationId);
    });

push.on('notification', function(data) {
    console.log("notification event");
     alert(JSON.stringify(data));
     });

push.on('error', function(e) {
    console.log("push error");
    alert(JSON.stringify(e));
});

function registerDeviceToken(deviceToken){
//Register the registrationId or deviceToken to your server as per the webservice type and parameters configuration set
}

Call the above code after the 'DeviceReady' event is fired in your Application. And the configurations in your AndroidManifest file would be (Reference Purpose):

<application>
<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.adobe.phonegap.push.PushHandlerActivity"
            android:exported="true" />

        <receiver android:name="com.adobe.phonegap.push.BackgroundActionButtonHandler" />
        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="yourPackageName" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.adobe.phonegap.push.GCMIntentService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name="com.adobe.phonegap.push.PushInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
        <service
            android:name="com.adobe.phonegap.push.RegistrationIntentService"
            android:exported="false" />
    </application>
<permission
        android:name="${applicationId}.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE"  />

    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

These configurations get added automatically in AndroidManifest whenever the plugin is installed. And in iOS, turn on the Push Notification service under capabilities section of your Project. Also ensure to share the correct Api key from Google Developer Console for Android and p12 file of Apple Push Notification service alongwith its password for iOS to server team to avoid Configuration Loopholes.

Namrata Bagerwal
  • 1,202
  • 13
  • 27