/ Published in: JavaScript
Implementing PubNub (http://www.pubnub.com) notifications for Titanium Android.
Also employs the Titanium Redux (https://github.com/dawsontoth/Appcelerator-Titanium-Redux) library.
Also employs the Titanium Redux (https://github.com/dawsontoth/Appcelerator-Titanium-Redux) library.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//////////////////////////////////// //app.js. //Application entry point. //////////////////////////////////// //////////////////////////////////// //Imports. //////////////////////////////////// //Redux.js. Titanium.include('jslibs/redux.js'); //PubNub library. include('jslibs/pubnub.js'); //////////////////////////////////// //////////////////////////////////// //RJSS style sheet. //////////////////////////////////// includeRJSSGlobal('rjss/common.rjss'); //////////////////////////////////// //////////////////////////////////// //Class includes. //////////////////////////////////// var classincludes = [ Titanium.UI.createLabel, Titanium.UI.createWindow, Titanium.UI.createButton, Titanium.UI.createImageView, Titanium.UI.createAnimation, Titanium.UI.createView, Titanium.UI.createActivityIndicator, Titanium.UI.createNotification, Titanium.UI.createTextField ]; //////////////////////////////////// //Main. //////////////////////////////////// /////////////////////////// //Global functions. /////////////////////////// //Floating point number precision. function handlefloat(num, precpoint) { return (parseFloat(num.toPrecision(precpoint))); } /////////////////////////// //////////////////////////////////// //Create root window. //Global vars. //////////////////////////////////// var rootwin = new Window({ title:'PubNubTest', id: 'rootwin', exitOnClose: true, scrollable: true, modal:true, animated:true }); rootwin.open(); rootwin.addEventListener('android:back', function() { rootwin.close(); }); /////////////////////////// //PubNub Object. /////////////////////////// var PubNub = { //<-- Begin, PubNub Object. /////////////////////////// //Constructor. /////////////////////////// init: function() { this.datain(); this.dataout(); this.sendbutton(); this.activityind('Loading'); PubNub.pubnubvars.actind.show(); }, /////////////////////////// /////////////////////////// //Object variables. /////////////////////////// /////////////////////////// /////////////////////////// //Variable holder. /////////////////////////// pubnubvars: new Object({}), /////////////////////////// //PubNub Initialize. /////////////////////////// pubnub : PUBNUB.init({ publish_key : 'demo', subscribe_key : 'demo', ssl : false, origin : 'pubsub.pubnub.com' }), /////////////////////////// /////////////////////////// //Main Methods. /////////////////////////// /////////////////////////// /////////////////////////// //PubNub Subscribe/Listen. /////////////////////////// pubnubsubscribe : function() { PubNub.pubnub.subscribe({ channel : 'test_channel', callback : function(message) { PubNub.pubnubvars.actind.hide(); //Output to label. PubNub.pubnubvars.datalabel.text = message.message; //Message Received. PubNub.notice('JSON String received: ' + JSON.stringify(message)); info(JSON.stringify(message)); }, error : function() { //Connection lost. info("Connection Lost"); } }) }, /////////////////////////// //PubNub Publish. /////////////////////////// pubnubpublish : function(msg) { PubNub.pubnub.publish({ channel : 'test_channel', message : { message : msg }, callback : function(inf) { if (inf[0]) { info("Successfully Sent Message!"); } else { //Connection lost. info("Failed! -> " + inf[1]); } } }) }, /////////////////////////// //PubNub History. /////////////////////////// pubnubhistory: function(txtfield) { PubNub.pubnub.history({ channel : 'test_channel', limit : 5, callback : function(messages) { PubNub.pubnubvars.actind.hide(); txtfield.text = 'Message History(\u5C65\u6B74):\n\n'; for(var m = 0; m < messages.length; m++) { txtfield.text += messages[m].message; } } }); }, /////////////////////////// /////////////////////////// //Publish to PubNub. //Send input. /////////////////////////// sendbutton: function() { var sendbutton = new Button({ id: 'sendbutton' }); rootwin.add(sendbutton); //Event handler. $(sendbutton).click(function() { PubNub.pubnubvars.actind.show(); //Publish to PubNub. var msg = PubNub.pubnubvars.inputbox.value; PubNub.pubnubpublish(msg); }); }, /////////////////////////// //PubNub Subscribe/Listen. //Display output. /////////////////////////// dataout: function() { //Listen for PubNub responses. PubNub.pubnubsubscribe(); //Output Label. var datalabel = new Label({ id: 'datalabel' }); //Output Box. var databox = new View({ id: 'databox' }); rootwin.add(databox); databox.add(datalabel); PubNub.pubnubvars.datalabel = datalabel; //Display message history. var historylabel = new Label({ id: 'historylabel' }); databox.add(historylabel); PubNub.pubnubhistory(historylabel); }, /////////////////////////// //Input text message. /////////////////////////// datain: function() { var inputbox = new TextField({ id: 'inputbox' }); rootwin.add(inputbox); PubNub.pubnubvars.inputbox = inputbox; }, /////////////////////////// //Create 'toast' notifications. /////////////////////////// notice: function(noticetxt) { var toast = Titanium.UI.createNotification({ duration: Titanium.UI.NOTIFICATION_DURATION_SHORT }); toast.show(); toast.message = noticetxt; }, /////////////////////////// //Create Activity indicator. /////////////////////////// activityind: function(msg) { var actind = new ActivityIndicator({ className: 'activity_indicator', message: msg }); rootwin.add(actind); PubNub.pubnubvars.actind = actind; } /////////////////////////// } //<-- End, PubNub Object. /////////////////////////// //Initialize application. /////////////////////////// PubNub.init(); ////////////////////////////////////