Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. var Cloud = require("ti.cloud");
  2.  
  3. Cloud.Users.login({
  4. login : 'YOUR_LOGIN',
  5. password : 'password'
  6. }, function(e) {
  7. if (e.success) {
  8. var user = e.users[0];
  9. Ti.API.info('Success:\n' + 'id: ' + user.id + '\n' + 'sessionId: ' + Cloud.sessionId + '\n' + 'first name: ' + user.first_name + '\n' + 'last name: ' + user.last_name);
  10.  
  11. } else {
  12. Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
  13. }
  14. });
  15.  
  16. var win = Ti.UI.createWindow({
  17. backgroundColor : '#fff'
  18. });
  19.  
  20. // Create a Button.
  21. var subscribed = Ti.UI.createButton({
  22. title : 'subscribed',
  23. height : Ti.UI.SIZE,
  24. width : Ti.UI.SIZE,
  25. top : 20,
  26.  
  27. });
  28.  
  29. // Listen for click events.
  30. subscribed.addEventListener('click', function() {
  31. subscribeToChannel();
  32. });
  33.  
  34. // Add to the parent view.
  35. win.add(subscribed);
  36.  
  37. var deviceToken = null;
  38.  
  39. var CloudPush = require('ti.cloudpush');
  40.  
  41. // Initialize the module
  42. CloudPush.retrieveDeviceToken({
  43. success : deviceTokenSuccess,
  44. error : deviceTokenError
  45. });
  46. // Enable push notifications for this device
  47. // Save the device token for subsequent API calls
  48. function deviceTokenSuccess(e) {
  49. deviceToken = e.deviceToken;
  50. }
  51.  
  52. function deviceTokenError(e) {
  53. Ti.API.info('Failed to register for push notifications! ' + e.error);
  54. }
  55.  
  56. // Process incoming push notifications
  57. CloudPush.addEventListener('callback', function(evt) {
  58. Ti.API.info("Notification received: " + evt.payload);
  59. });
  60.  
  61. // Check if the device is running iOS 8 or later
  62.  
  63. // Process incoming push notifications
  64. function receivePush(e) {
  65. Ti.API.info('Received push: ' + JSON.stringify(e));
  66. }
  67.  
  68. // Save the device token for subsequent API calls
  69. function deviceTokenSuccess(e) {
  70. deviceToken = e.deviceToken;
  71.  
  72. }
  73.  
  74. function deviceTokenError(e) {
  75. Ti.API.info('Failed to register for push notifications! ' + e.error);
  76. }
  77.  
  78. function subscribeToChannel() {
  79. // Subscribes the device to the 'news_alerts' channel
  80. // Specify the push type as either 'android' for Android or 'ios' for iOS
  81. Cloud.PushNotifications.subscribeToken({
  82. device_token : deviceToken,
  83. channel : 'update_alart',
  84. type : Ti.Platform.name == 'android' ? 'android' : 'ios'
  85. }, function(e) {
  86. if (e.success) {
  87. Ti.API.info('Subscribed');
  88.  
  89. } else {
  90. Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
  91. }
  92. });
  93. }
  94.  
  95. win.open();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement