5

I am trying to use the Pushwoosh Push Notifications phonegap build plugin but the registerDevice callbacks never fires on my android device, however the initPushwoosh function DOES fire because I see the alert from the call to alert("initPushwoosh" ) in the code below

Below is my config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- config.xml reference: https://build.phonegap.com/docs/config-xml -->
<widget xmlns     = "http://www.w3.org/ns/widgets"
        xmlns:gap = "http://phonegap.com/ns/1.0"
        id        = "com.phonegap.hello-world"
        version   = "1.0.0">

    <name>Push notification</name>

    <description>
        Camera example app.
    </description>

    <author href="http://phonegap.com" email="support@phonegap.com">
        PhoneGap Team
    </author>

    <preference name="phonegap-version" value="3.3.0" /> 

    <gap:plugin name="org.apache.cordova.core.camera" />

     <plugin name="PushNotification"
        value="com.pushwoosh.plugin.pushnotifications.PushNotifications" onload="true"/>

     <access origin="*.pushwoosh.com" />
</widget>

and here is my index.js, I just am changing the project id and appid to XXXX so that I don't mistakenly reveal too much.

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        alert("onDeviceReady" );
        app.initPushwoosh();
        app.receivedEvent('deviceready');

    },

    initPushwoosh: function() {
        alert("initPushwoosh" );
            var pushNotification = window.plugins.pushNotification;
            pushNotification.registerDevice(
            { projectid: "XXX-XXX-XXXX 3", appid : "XXXXX-XXXXX" },
                function(status) {
                    var pushToken = status;
                    alert('push token: ' + pushToken);
                },
                function(status) {
                    alert(JSON.stringify(['failed to register ', status]));
            });
            document.addEventListener('push-notification', function(event) {            
                var title = event.notification.title;
                 var userData = event.notification.userdata;
                if (typeof(userData) != "undefined") {
                   alert('user data: ' + JSON.stringify(userData));
                }    
                navigator.notification.alert(title);
            });
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },

    takePicture: function() {
      navigator.camera.getPicture( function( imageURI ) {
        alert( imageURI );
      },
      function( message ) {
        alert( message );
      },
      {
        quality: 50,
        destinationType: Camera.DestinationType.FILE_URI
      });
    }
};

any help would be greatly appreciated... thanks!

gabriel
  • 326
  • 1
  • 2
  • 12

2 Answers2

1

Cordova has replaced the window.plugins with the function cordova.require() You need to look for the namespace in which the plugin is defined. For pushwoosh it's: "com.pushwoosh.plugins.pushwoosh.PushNotification"

So instead of:

var PushNotification = window.plugins.PushNotification;

try this:

var PushNotification = cordova.require("com.pushwoosh.plugins.pushwoosh.PushNotification");
0

Do you use the latest version of the plugin? If so please check the latest guide: http://www.pushwoosh.com/programming-push-notification/android/android-additional-platforms/phonegapcordova-sdk-integration/

It has been changed recently and initPushwoosh function looks like this:

function initPushwoosh()
{
    var pushNotification = window.plugins.pushNotification;

    //set push notifications handler
    document.addEventListener('push-notification', function(event) {
        var title = event.notification.title;
        var userData = event.notification.userdata;

        if(typeof(userData) != "undefined") {
            console.warn('user data: ' + JSON.stringify(userData));
        }

        navigator.notification.alert(title);
    });

    //initialize Pushwoosh with projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID". This will trigger all pending push notifications on start.
    pushNotification.onDeviceReady({ projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID" });

    //register for pushes
    pushNotification.registerDevice(
        function(status) {
            var pushToken = status;
            console.warn('push token: ' + pushToken);
        },
        function(status) {
            console.warn(JSON.stringify(['failed to register ', status]));
        }
    );
}

Also you might want to double check AndroidManifest.xml. Unfortunately it's really easy to make it wrong :((

shader
  • 2,121
  • 1
  • 13
  • 20
  • 1
    Shader, I am using phonegap build so my understanding is that all I need to do is add these 2 lines (below) to the config.xl file and that phonegap build will have use the latest pushwoosh plugin and also create the AdroidManifest.xml file for me... have you used phonegap build successfully for this? – gabriel May 19 '14 at 12:51
  • the problem is that windows.plugins is undefined, so obviously window.plugins.PushNotification is undefined and the app blows up on this line var pushNotification = window.plugins.PushNotification; – gabriel May 19 '14 at 22:15
  • I see, for PGB please use the original code, the plugin on PGB is not updated yet (takes ages with PGB team). In config.xml your entry is incorrect then. It should be as : http://www.pushwoosh.com/programming-push-notification/android/android-additional-platforms/phonegap-build/ – shader May 20 '14 at 10:02
  • I tried using both in my config.xml and I get the same error of window.splugin is not defined with either one – gabriel May 20 '14 at 16:46