Advertisement
Guest User

Untitled

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