Posted By

StrawMan on 11/15/11


Tagged

Titanium Appcelerator


Versions (?)

PubNub Test - Appcelerator Titanium/Android


 / 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.

  1. ////////////////////////////////////
  2. //app.js.
  3. //Application entry point.
  4. ////////////////////////////////////
  5.  
  6. ////////////////////////////////////
  7. //Imports.
  8. ////////////////////////////////////
  9. //Redux.js.
  10. Titanium.include('jslibs/redux.js');
  11. //PubNub library.
  12. include('jslibs/pubnub.js');
  13. ////////////////////////////////////
  14.  
  15. ////////////////////////////////////
  16. //RJSS style sheet.
  17. ////////////////////////////////////
  18. includeRJSSGlobal('rjss/common.rjss');
  19. ////////////////////////////////////
  20.  
  21. ////////////////////////////////////
  22. //Class includes.
  23. ////////////////////////////////////
  24. var classincludes = [
  25. Titanium.UI.createLabel,
  26. Titanium.UI.createWindow,
  27. Titanium.UI.createButton,
  28. Titanium.UI.createImageView,
  29. Titanium.UI.createAnimation,
  30. Titanium.UI.createView,
  31. Titanium.UI.createActivityIndicator,
  32. Titanium.UI.createNotification,
  33. Titanium.UI.createTextField
  34. ];
  35.  
  36. ////////////////////////////////////
  37. //Main.
  38. ////////////////////////////////////
  39.  
  40. ///////////////////////////
  41. //Global functions.
  42. ///////////////////////////
  43. //Floating point number precision.
  44. function handlefloat(num, precpoint) {
  45. return (parseFloat(num.toPrecision(precpoint)));
  46. }
  47. ///////////////////////////
  48.  
  49. ////////////////////////////////////
  50. //Create root window.
  51. //Global vars.
  52. ////////////////////////////////////
  53. var rootwin = new Window({
  54. title:'PubNubTest',
  55. id: 'rootwin',
  56. exitOnClose: true,
  57. scrollable: true,
  58. modal:true,
  59. animated:true
  60. });
  61. rootwin.open();
  62. rootwin.addEventListener('android:back', function() {
  63. rootwin.close();
  64. });
  65.  
  66. ///////////////////////////
  67. //PubNub Object.
  68. ///////////////////////////
  69. var PubNub = { //<-- Begin, PubNub Object.
  70.  
  71. ///////////////////////////
  72. //Constructor.
  73. ///////////////////////////
  74. init: function() {
  75. this.datain();
  76. this.dataout();
  77. this.sendbutton();
  78. this.activityind('Loading');
  79. PubNub.pubnubvars.actind.show();
  80. },
  81.  
  82. ///////////////////////////
  83. ///////////////////////////
  84. //Object variables.
  85. ///////////////////////////
  86. ///////////////////////////
  87.  
  88. ///////////////////////////
  89. //Variable holder.
  90. ///////////////////////////
  91. pubnubvars: new Object({}),
  92. ///////////////////////////
  93. //PubNub Initialize.
  94. ///////////////////////////
  95. pubnub : PUBNUB.init({
  96. publish_key : 'demo',
  97. subscribe_key : 'demo',
  98. ssl : false,
  99. origin : 'pubsub.pubnub.com'
  100. }),
  101.  
  102. ///////////////////////////
  103. ///////////////////////////
  104. //Main Methods.
  105. ///////////////////////////
  106. ///////////////////////////
  107.  
  108. ///////////////////////////
  109. //PubNub Subscribe/Listen.
  110. ///////////////////////////
  111. pubnubsubscribe : function() {
  112. PubNub.pubnub.subscribe({
  113. channel : 'test_channel',
  114. callback : function(message) {
  115. PubNub.pubnubvars.actind.hide();
  116. //Output to label.
  117. PubNub.pubnubvars.datalabel.text = message.message;
  118. //Message Received.
  119. PubNub.notice('JSON String received: ' + JSON.stringify(message));
  120. info(JSON.stringify(message));
  121. },
  122. error : function() {
  123. //Connection lost.
  124. info("Connection Lost");
  125. }
  126. })
  127. },
  128. ///////////////////////////
  129. //PubNub Publish.
  130. ///////////////////////////
  131. pubnubpublish : function(msg) {
  132. PubNub.pubnub.publish({
  133. channel : 'test_channel',
  134. message : {
  135. message : msg
  136. },
  137. callback : function(inf) {
  138. if (inf[0]) {
  139. info("Successfully Sent Message!");
  140. } else {
  141. //Connection lost.
  142. info("Failed! -> " + inf[1]);
  143. }
  144. }
  145. })
  146. },
  147.  
  148. ///////////////////////////
  149. //PubNub History.
  150. ///////////////////////////
  151. pubnubhistory: function(txtfield) {
  152. PubNub.pubnub.history({
  153. channel : 'test_channel',
  154. limit : 5,
  155. callback : function(messages) {
  156. PubNub.pubnubvars.actind.hide();
  157. txtfield.text = 'Message History(\u5C65\u6B74):\n\n';
  158. for(var m = 0; m < messages.length; m++) {
  159. txtfield.text += messages[m].message;
  160. }
  161. }
  162. });
  163. },
  164. ///////////////////////////
  165.  
  166. ///////////////////////////
  167. //Publish to PubNub.
  168. //Send input.
  169. ///////////////////////////
  170. sendbutton: function() {
  171. var sendbutton = new Button({
  172. id: 'sendbutton'
  173. });
  174. rootwin.add(sendbutton);
  175. //Event handler.
  176. $(sendbutton).click(function() {
  177. PubNub.pubnubvars.actind.show();
  178. //Publish to PubNub.
  179. var msg = PubNub.pubnubvars.inputbox.value;
  180. PubNub.pubnubpublish(msg);
  181. });
  182. },
  183. ///////////////////////////
  184. //PubNub Subscribe/Listen.
  185. //Display output.
  186. ///////////////////////////
  187. dataout: function() {
  188. //Listen for PubNub responses.
  189. PubNub.pubnubsubscribe();
  190. //Output Label.
  191. var datalabel = new Label({
  192. id: 'datalabel'
  193. });
  194. //Output Box.
  195. var databox = new View({
  196. id: 'databox'
  197. });
  198. rootwin.add(databox);
  199. databox.add(datalabel);
  200. PubNub.pubnubvars.datalabel = datalabel;
  201. //Display message history.
  202. var historylabel = new Label({
  203. id: 'historylabel'
  204. });
  205. databox.add(historylabel);
  206. PubNub.pubnubhistory(historylabel);
  207. },
  208. ///////////////////////////
  209. //Input text message.
  210. ///////////////////////////
  211. datain: function() {
  212. var inputbox = new TextField({
  213. id: 'inputbox'
  214. });
  215. rootwin.add(inputbox);
  216. PubNub.pubnubvars.inputbox = inputbox;
  217. },
  218. ///////////////////////////
  219. //Create 'toast' notifications.
  220. ///////////////////////////
  221. notice: function(noticetxt) {
  222. var toast = Titanium.UI.createNotification({
  223. duration: Titanium.UI.NOTIFICATION_DURATION_SHORT
  224. });
  225. toast.show();
  226. toast.message = noticetxt;
  227. },
  228. ///////////////////////////
  229. //Create Activity indicator.
  230. ///////////////////////////
  231. activityind: function(msg) {
  232. var actind = new ActivityIndicator({
  233. className: 'activity_indicator',
  234. message: msg
  235. });
  236. rootwin.add(actind);
  237. PubNub.pubnubvars.actind = actind;
  238. }
  239. ///////////////////////////
  240. } //<-- End, PubNub Object.
  241. ///////////////////////////
  242. //Initialize application.
  243. ///////////////////////////
  244. PubNub.init();
  245. ////////////////////////////////////

Report this snippet  

You need to login to post a comment.