Advertisement
Guest User

Untitled

a guest
Jun 17th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. var win = Titanium.UI.createWindow({
  2. backgroundColor : 'white',
  3. layout : "vertical"
  4. });
  5.  
  6. // Create a TextField.
  7. var channel = Ti.UI.createTextField({
  8. height : 35,
  9. top : 40,
  10. left : 20,
  11. right : 20,
  12. hintText : 'Enter your channel name',
  13. keyboardType : Ti.UI.KEYBOARD_DEFAULT,
  14. returnKeyType : Ti.UI.RETURNKEY_DEFAULT,
  15. borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED
  16. });
  17.  
  18. // Add to the parent view.
  19. win.add(channel);
  20.  
  21. // Create a Button.
  22. var subscribe = Ti.UI.createButton({
  23. title : 'subscribe',
  24. height : Ti.UI.SIZE,
  25. width : Ti.UI.SIZE,
  26. top : 20
  27.  
  28. });
  29.  
  30. // Add to the parent view.
  31. win.add(subscribe);
  32.  
  33. // Create a Button.
  34. var unsubscribe = Ti.UI.createButton({
  35. title : 'unsubscribe',
  36. height : Ti.UI.SIZE,
  37. width : Ti.UI.SIZE,
  38. top : 20,
  39.  
  40. });
  41.  
  42. // Add to the parent view.
  43. win.add(unsubscribe);
  44.  
  45. var Cloud = require("ti.cloud");
  46. var deviceToken = null;
  47.  
  48. Cloud.Users.login({
  49. login : 'iphone@gmail.com',
  50. password : '1234'
  51. }, function(e) {
  52. if (e.success) {
  53. var user = e.users[0];
  54. alert(' Login Success:\n' + 'id: ' + user.id + '\n' + 'sessionId: ' + Cloud.sessionId + '\n' + 'first name: ' + user.first_name + '\n' + 'last name: ' + user.last_name);
  55.  
  56. } else {
  57. alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
  58. }
  59. });
  60.  
  61. if (Ti.Platform.osname === "android") {
  62. var CloudPush = require('ti.cloudpush');
  63.  
  64. // Initialize the module
  65. CloudPush.retrieveDeviceToken({
  66. success : deviceTokenSuccess,
  67. error : deviceTokenError
  68. });
  69. // Enable push notifications for this device
  70. // Save the device token for subsequent API calls
  71. function deviceTokenSuccess(e) {
  72. deviceToken = e.deviceToken;
  73. }
  74.  
  75. function deviceTokenError(e) {
  76. Ti.API.info('Failed to register for push notifications! ' + e.error);
  77. }
  78.  
  79. // Process incoming push notifications
  80. CloudPush.addEventListener('callback', function(evt) {
  81. Ti.API.info("Notification received: " + evt.payload);
  82. });
  83.  
  84. } else {
  85. if (Ti.Platform.name == "iPhone OS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
  86.  
  87. // Wait for user settings to be registered before registering for push notifications
  88. Ti.App.iOS.addEventListener('usernotificationsettings', function registerForPush() {
  89.  
  90. // Remove event listener once registered for push notifications
  91. Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
  92.  
  93. Ti.Network.registerForPushNotifications({
  94. success : deviceTokenSuccess,
  95. error : deviceTokenError,
  96. callback : receivePush
  97. });
  98. });
  99.  
  100. // Register notification types to use
  101. Ti.App.iOS.registerUserNotificationSettings({
  102. types : [Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT, Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND, Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE]
  103. });
  104. }
  105.  
  106. // For iOS 7 and earlier
  107. else {
  108. Ti.Network.registerForPushNotifications({
  109. // Specifies which notifications to receive
  110. types : [Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND],
  111. success : deviceTokenSuccess,
  112. error : deviceTokenError,
  113. callback : receivePush
  114. });
  115.  
  116. }
  117. }
  118.  
  119. // Check if the device is running iOS 8 or later
  120.  
  121. // Process incoming push notifications
  122. function receivePush(e) {
  123. Ti.API.info('Received push: ' + JSON.stringify(e));
  124. }
  125.  
  126. // Save the device token for subsequent API calls
  127. function deviceTokenSuccess(e) {
  128. deviceToken = e.deviceToken;
  129.  
  130. }
  131.  
  132. function deviceTokenError(e) {
  133. Ti.API.info('Failed to register for push notifications! ' + e.error);
  134. }
  135.  
  136. function subscribeToChannel() {
  137. // Subscribes the device to the 'news_alerts' channel
  138. // Specify the push type as either 'android' for Android or 'ios' for iOS
  139. if (channel.getValue() == '') {
  140. alert('please insert channel name');
  141.  
  142. } else {
  143.  
  144. Cloud.PushNotifications.subscribe({
  145. channel : channel.getValue(),
  146. device_token : deviceToken,
  147. type : Ti.Platform.name == 'android' ? 'android' : 'ios'
  148. }, function(e) {
  149. if (e.success) {
  150. alert('Successfully subscribed');
  151. } else {
  152. alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
  153. }
  154. });
  155. }
  156. }
  157.  
  158. function unsubscribeToChannel() {
  159. // Subscribes the device to the 'news_alerts' channel
  160. // Specify the push type as either 'android' for Android or 'ios' for iOS
  161. if (channel.getValue() == '') {
  162. alert('please insert channel name');
  163. } else {
  164.  
  165. Cloud.PushNotifications.unsubscribe({
  166. channel : channel.getValue(),
  167. device_token : deviceToken
  168. }, function(e) {
  169. if (e.success) {
  170. alert('Successfully unsubscribed');
  171. } else {
  172. alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
  173. }
  174. });
  175. }
  176. }
  177.  
  178. // Listen for click events.
  179. subscribe.addEventListener('click', function() {
  180. subscribeToChannel();
  181. });
  182.  
  183. // Listen for click events.
  184. unsubscribe.addEventListener('click', function() {
  185. unsubscribeToChannel();
  186. });
  187.  
  188. win.open();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement