The community forums are in BETA and closed to the public.

Phonegap Android GCM Push Notification: Error: PnMessage is undefined

I am building chat application in phonegap using GCM and Pubnub. I referred this article http://www.pubnub.com/blog/sending-android-push-notifications-via-gcm-javascript-using-phonegap/

My browser console is showing PnMessage is undefined. I have checked in pubnub.js file. It is defined there. So what is the issue then? These are my files:-

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
    <script src="index.js"></script>
    <script src="pubnub-3.7.6.min.js"></script>
</head>

<body onload="onLoad()">
    <h1>This is a Heading</h1>  <p>This is a paragraph.</p>
</body>
</html>

index.js

var pubnub;
function onLoad(){
    pubnub = PUBNUB.init({ 
        subscribe_key: 'sub-c-2dc2f5b4-8d29-...........', 
        publish_key: 'pub-c-124ec6e8-c.......'
    });

    pubnub.publish({
        channel: 'Doctor',
        message: {"message":"phonegap onDeviceReady"}
    });

    sendPush();     
}

function  sendPush() { 
    pubnub.mobile_gw_provision ({ 
        device_id: 'APA91bFWRqK3ItjGduf3UZKbxbn_.........', // Reg ID you got on your device 
        channel : 'Doctor', 
        op: 'add', 
        gw_type: 'gcm', 
        error : function(msg){ console.log(msg); }, 
        callback : successCallback 
    }); 
}

function  successCallback() { 
    console.log('successCallback');

    var message = PnMessage();
    message.pubnub = pubnub; 
    message.callback = function(msg){ console.log(msg); }; 
    message.error = function (msg){ console.log(msg); }; 
    message.channel = 'Doctor'; 

    message.gcm = { 
        title: 'Push Demo', 
        message: 'The room temperature is set too high' 
    };

    message.publish(); 
}

Got the issue solved. min version of pubnub.js doesn't has the PnMessage class. we have to use the bigger version of pubnub.js

Reformatted the code above as some was not displaying at all.

I'll have to verify but the sendPush method is just registering a device for push notification but you are calling pubnub.publish before this device is registered. Register the device first then publish - and make sure the device has completed registering before publish is called (async warning).

Yeah, my analysis didn't take into account your successCallback function. The minified version on cdn should have been updated by now. Your local copy could have just been a day or two out of date, maybe?

Either way, glad it is working for you know.

Thanks Craig for your answer. However we are still using original version of http://cdn.pubnub.com/pubnub-3.7.6.js. http://cdn.pubnub.com/pubnub-3.7.6.min.js still doesn't has the PnMEssage class.

It's in there but it is spelled PNmessage but your code uses PnMessage (uppercase M). Give that a fix and let me know if you still have issues.

:( Not working. What I am doing: Taking a local copy of http://cdn.pubnub.com/pubnub-3.7.6.min.js I have tried both PNmessage() and PnMessage(). Both are showing undefined in the web-app.

When I am trying to search for PNmessage in http://cdn.pubnub.com/pubnub-3.7.6.min.js. I only got one search result: newPnMessage:function.....

Forget 3.7.6. We just updated the CDNs with 3.7.8 that has all the latest from github repo.

Please use the following and let me know if you continue to have issues:

NOTE: versions updated to current latest (3.7.8) but always check for latest version on our JavaScript SDK github repo.


Comments to this discussion are now closed!