Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- console.log('### APP STARTED');
- var Cloud = require('ti.cloud');
- var initNotifications = function (callback) {
- console.log('### init notifications');
- var CloudPush = require('ti.cloudpush');
- CloudPush.singleCallback = true;
- CloudPush.showTrayNotificationsWhenFocused = false;
- CloudPush.retrieveDeviceToken({
- success: function (e) {
- console.log('### deviceTokenSuccess: ' + e.deviceToken);
- if (!Ti.App.Properties.getBool('subscribed')) {
- console.log('### will subscribe');
- Cloud.PushNotifications.subscribe({
- channel: 'test',
- device_token: e.deviceToken,
- type: 'android'
- }, function (e) {
- console.log('### subscribe result: ' + JSON.stringify(e));
- if (e.success) {
- Ti.App.Properties.setBool('subscribed', true);
- }
- });
- }
- },
- error: function (e) {
- console.log('### deviceTokenError: ' + JSON.stringify(e));
- }
- });
- CloudPush.addEventListener('callback', function (evt) {
- alert('notification received: ' + JSON.stringify(evt.payload));
- });
- };
- if (!Ti.App.Properties.getBool('user_created')) {
- console.log('### will create user');
- Cloud.Users.create({
- username: 'test',
- password: 'test',
- password_confirmation: 'test',
- }, function (e_create) {
- console.log('### result: ' + JSON.stringify(e_create));
- if (e_create.success) {
- Ti.App.Properties.setBool('user_created', true);
- initNotifications();
- } else if (e_create.message.indexOf('Username is already taken') >= -1) {
- initNotifications();
- } else {
- alert('Error: ' + JSON.stringify(e_create));
- }
- });
- } else {
- console.log('### user already created');
- initNotifications();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement