push.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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(data) {
  32. console.log(JSON.stringify(data));
  33. if(data.additionalData.type === 'alarm')
  34. {
  35. /*
  36. * QUESTA è una COMUNICAZIONE IMPORTANTE!
  37. */
  38. $("body").pagecontainer("change", "#alert");
  39. $("#alert-title").html(data.title);
  40. $("#alert-message").html(data.message);
  41. }
  42. else
  43. {
  44. /*
  45. * QUESTO è un SEMPLICE AGGIORNAMENTO!
  46. */
  47. if(!empty(data.additionalData.url))
  48. {
  49. console.log('HO UN URL VALIDO '+data.additionalData.url);
  50. }
  51. else {
  52. console.log('NESSUN URL, DEVO VISUALIZZARE DA QUALCHE PARTE MESSAGGIO E TITOLO..');
  53. }
  54. }
  55. });
  56. push.on('error', function(e) {
  57. console.log(JSON.stringify(e));
  58. });
  59. };
  60. document.addEventListener('deviceready', onDeviceReadyPush, true);