push.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function onDeviceReadyPush() {
  2. var push = PushNotification.init({
  3. // forceReload: true,
  4. // forceStart: true,
  5. android: {
  6. senderID: "667898382143"
  7. },
  8. browser: {
  9. pushServiceURL: 'http://push.api.phonegap.com/v1/push'
  10. },
  11. ios: {
  12. alert: "true",
  13. badge: "true",
  14. sound: "true"
  15. },
  16. windows: {}
  17. });
  18. /*
  19. * registra l'id univoco dell'installazione su xm.bus.pm
  20. */
  21. push.on('registration', function(registration) {
  22. console.log(JSON.stringify(registration));
  23. var url = "https://xm.bus.pm/api/d/c?token=" + registration.registrationId + "&token_type=gcm&topic=xm24";
  24. $.get(url, function( data ) {
  25. console.log(JSON.stringify(data));
  26. });
  27. });
  28. /*
  29. * gestisce la notifica ricevuta
  30. */
  31. push.on('notification', function(notification) {
  32. console.log(JSON.stringify(notification));
  33. switch(notification.additionalData.type) {
  34. case 'event':
  35. /*
  36. * Questo è il reminder o la pubblicazione di un evento
  37. */
  38. var eventId = notification.additionalData.data.id;
  39. var eventUrl = notification.additionalData.url;
  40. /*
  41. * TODO: mandare l'utente all'evento..
  42. * l'url servirebbe solo se lo si vuole fare in webview
  43. * altrimenti bisognerebbe fare il fetch dell'xml dal sito,
  44. * salvarlo o comunque visualizzare la pagina
  45. */
  46. break;
  47. case 'post':
  48. /*
  49. * Questo è il reminder o la pubblicazione di un post
  50. */
  51. var postId = notification.additionalData.data.id;
  52. var postUrl = notification.additionalData.url;
  53. /*
  54. * TODO: mandare l'utente al post..
  55. * l'url servirebbe solo se lo si vuole fare in webview
  56. * altrimenti bisognerebbe fare il fetch dell'xml o json dal sito,
  57. * salvarlo o comunque visualizzare la pagina
  58. */
  59. break;
  60. case 'notice':
  61. /*
  62. * Questa è una comunicazione generica
  63. */
  64. // TODO: cosa mettere nel payload? Che fare?
  65. break;
  66. case 'alarm':
  67. /*
  68. * Questa è una comunicazione importante
  69. */
  70. // TODO: cosa mettere nel payload? Che fare? Qui per esempio mostro semplicemente il messaggio
  71. $("body").pagecontainer("change", "#alert");
  72. $("#alert-title").html(notification.title);
  73. $("#alert-message").html(notification.message);
  74. break;
  75. }
  76. });
  77. push.on('error', function(e) {
  78. console.log(JSON.stringify(e));
  79. });
  80. }
  81. document.addEventListener('deviceready', onDeviceReadyPush, true);