Advertisement
Guest User

Untitled

a guest
Mar 17th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. var Cloud = require("ti.cloud");
  2. var deviceToken = null;
  3.  
  4. if (Ti.Platform.osname === "android") {
  5. var CloudPush = require('ti.cloudpush');
  6.  
  7. // Initialize the module
  8. CloudPush.retrieveDeviceToken({
  9. success : deviceTokenSuccess,
  10. error : deviceTokenError
  11. });
  12. // Enable push notifications for this device
  13. // Save the device token for subsequent API calls
  14. function deviceTokenSuccess(e) {
  15. deviceToken = e.deviceToken;
  16. }
  17.  
  18. function deviceTokenError(e) {
  19. Ti.API.info('Failed to register for push notifications! ' + e.error);
  20. }
  21.  
  22. // Process incoming push notifications
  23. CloudPush.addEventListener('callback', function(evt) {
  24. Ti.API.info("Notification received: " + evt.payload);
  25. });
  26.  
  27. } else {
  28. if (Ti.Platform.name == "iPhone OS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
  29.  
  30. // Wait for user settings to be registered before registering for push notifications
  31. Ti.App.iOS.addEventListener('usernotificationsettings', function registerForPush() {
  32.  
  33. // Remove event listener once registered for push notifications
  34. Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
  35.  
  36. Ti.Network.registerForPushNotifications({
  37. success : deviceTokenSuccess,
  38. error : deviceTokenError,
  39. callback : receivePush
  40. });
  41. });
  42.  
  43. // Register notification types to use
  44. Ti.App.iOS.registerUserNotificationSettings({
  45. types : [Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT, Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND, Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE]
  46. });
  47. }
  48.  
  49. // For iOS 7 and earlier
  50. else {
  51. Ti.Network.registerForPushNotifications({
  52. // Specifies which notifications to receive
  53. types : [Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND],
  54. success : deviceTokenSuccess,
  55. error : deviceTokenError,
  56. callback : receivePush
  57. });
  58.  
  59. }
  60. }
  61.  
  62. // Check if the device is running iOS 8 or later
  63.  
  64. // Process incoming push notifications
  65. function receivePush(e) {
  66. Ti.API.info('Received push: ' + JSON.stringify(e));
  67. }
  68.  
  69. // Save the device token for subsequent API calls
  70. function deviceTokenSuccess(e) {
  71. deviceToken = e.deviceToken;
  72.  
  73. }
  74.  
  75. function deviceTokenError(e) {
  76. Ti.API.info('Failed to register for push notifications! ' + e.error);
  77. }
  78.  
  79. function subscribeToChannel() {
  80. // Subscribes the device to the 'news_alerts' channel
  81. // Specify the push type as either 'android' for Android or 'ios' for iOS
  82. Cloud.PushNotifications.subscribeToken({
  83. device_token : deviceToken,
  84. channel : 'update_alart',
  85. type : Ti.Platform.name == 'android' ? 'android' : 'ios'
  86. }, function(e) {
  87. if (e.success) {
  88. Ti.API.info('Subscribed');
  89.  
  90. } else {
  91. Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
  92. }
  93. });
  94. }
  95.  
  96. Cloud.Users.login({
  97. login : 'motiur.mbstu@gmail.com',
  98. password : '1234'
  99. }, function(e) {
  100. if (e.success) {
  101. var user = e.users[0]; ('Success:\n' + 'id: ' + user.id + '\n' + 'sessionId: ' + Cloud.sessionId + '\n' + 'first name: ' + user.first_name + '\n' + 'last name: ' + user.last_name);
  102. subscribeToChannel();
  103. } else {
  104. Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
  105. }
  106. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement