Advertisement
Guest User

Untitled

a guest
Feb 14th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. console.log('### APP STARTED');
  2.  
  3. var Cloud = require('ti.cloud');
  4.  
  5. var initNotifications = function (callback) {
  6. console.log('### init notifications');
  7.  
  8. var CloudPush = require('ti.cloudpush');
  9. CloudPush.singleCallback = true;
  10. CloudPush.showTrayNotificationsWhenFocused = false;
  11.  
  12. CloudPush.retrieveDeviceToken({
  13. success: function (e) {
  14. console.log('### deviceTokenSuccess: ' + e.deviceToken);
  15.  
  16. if (!Ti.App.Properties.getBool('subscribed')) {
  17. console.log('### will subscribe');
  18. Cloud.PushNotifications.subscribe({
  19. channel: 'test',
  20. device_token: e.deviceToken,
  21. type: 'android'
  22. }, function (e) {
  23. console.log('### subscribe result: ' + JSON.stringify(e));
  24. if (e.success) {
  25. Ti.App.Properties.setBool('subscribed', true);
  26. }
  27. });
  28. }
  29. },
  30. error: function (e) {
  31. console.log('### deviceTokenError: ' + JSON.stringify(e));
  32. }
  33. });
  34.  
  35. CloudPush.addEventListener('callback', function (evt) {
  36. alert('notification received: ' + JSON.stringify(evt.payload));
  37. });
  38. };
  39.  
  40. if (!Ti.App.Properties.getBool('user_created')) {
  41. console.log('### will create user');
  42. Cloud.Users.create({
  43. username: 'test',
  44. password: 'test',
  45. password_confirmation: 'test',
  46. }, function (e_create) {
  47. console.log('### result: ' + JSON.stringify(e_create));
  48.  
  49. if (e_create.success) {
  50. Ti.App.Properties.setBool('user_created', true);
  51. initNotifications();
  52. } else if (e_create.message.indexOf('Username is already taken') >= -1) {
  53. initNotifications();
  54. } else {
  55. alert('Error: ' + JSON.stringify(e_create));
  56. }
  57. });
  58. } else {
  59. console.log('### user already created');
  60. initNotifications();
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement