Advertisement
Guest User

Untitled

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