Created
February 14, 2017 21:05
-
-
Save DirleiDionisio/1df92b027d3da1f0c9edf3ae58a1767b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment