Last active
October 7, 2015 12:27
-
-
Save clathrop/3164594 to your computer and use it in GitHub Desktop.
An example of how to set up everything you need for push notifications.
This file contains 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
/* ACS Push Notification Sample | |
* created by: Carter Lathrop | |
* | |
* This is a small sample app on using push notifications. In order to register your device for push notifcations | |
* there are a couple steps you must take to ensure your device is connected to the cloud. First, a user must | |
* be logged in. Then push notifications must be enabled, in this app there is a switch to enable/disable it. | |
* Then a user must subscribe to a channel. A channel is pipeline for sending notifications, designed for a | |
* way to send notifications through a certain channel so that only people subscribed to the channel "racecar", | |
* for example, will recieve notifications about racecars. In this example app though, there is only support | |
* for sending push notifications from the web server, a push from the web will go to all devices that are | |
* subscribed to at least one channel (that channel can be any string, 'racecar' for example will work fine). | |
*/ | |
Titanium.UI.setBackgroundColor('#000'); | |
var Cloud = require('ti.cloud'); | |
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | |
//HOME WINDOW | |
//BUTTONS AND UI STUFF | |
var title = Ti.UI.createLabel({ | |
color : '#C8C8C8', | |
font : { | |
fontSize : 40 | |
}, | |
text : 'Push Example App', | |
textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER, | |
top : '10dp', | |
height : 'auto', | |
width : 'auto' | |
}); | |
var win1 = Titanium.UI.createWindow({ | |
backgroundImage : 'pushexamplebg.png', | |
layout : 'vertical' | |
}); | |
var scrollView = Ti.UI.createScrollView({ | |
backgroundImage : 'pushexamplebg.png', | |
layout : 'vertical' | |
}); | |
var createUserButton = Ti.UI.createButton({ | |
title : 'Create User', | |
top : '200dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp' | |
}); | |
var loginButton = Ti.UI.createButton({ | |
title : 'Login', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp' | |
}); | |
var logoutButton = Ti.UI.createButton({ | |
title : 'Logout', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp' | |
}); | |
var enablePush = Ti.UI.createButton({ | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp', | |
title : pushNotificationsEnabled ? 'Enabled' : 'Disabled' | |
}); | |
var subscribeButton = Ti.UI.createButton({ | |
title : 'Subscribe', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp' | |
}); | |
var channel = Ti.UI.createTextField({ | |
hintText : 'Enter anything here and click subscribe', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp', | |
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED, | |
autocapitalization : Ti.UI.TEXT_AUTOCAPITALIZATION_NONE, | |
autocorrect : false | |
}); | |
var credit = Ti.UI.createLabel({ | |
color : '#A0A0A0', | |
font : { | |
fontSize : 18 | |
}, | |
text : 'by OriginalKopy', | |
//textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER, | |
bottom : '5dp', | |
right : '5dp', | |
height : 'auto', | |
width : 'auto' | |
}); | |
//LOGIC TO CHECK WHICH PLATFORM WE ARE ON | |
var androidPushModule = null; | |
var pushDeviceToken = null, pushNotificationsEnabled = null; | |
if (Ti.Platform.name === 'iPhone OS') { | |
/* | |
iOS doesn't let us make customizations here. | |
The user can do this from the settings for the application. | |
*/ | |
} else if (Ti.Platform.name === 'android') { | |
if (androidPushModule === null) { | |
androidPushModule = getAndroidPushModule(); | |
//if (androidPushModule === null) { | |
//return; | |
} | |
} | |
//ADD UI TO WIN1 | |
scrollView.add(title); | |
scrollView.add(enablePush); | |
scrollView.add(subscribeButton); | |
scrollView.add(channel); | |
scrollView.add(createUserButton); | |
scrollView.add(loginButton); | |
scrollView.add(logoutButton); | |
scrollView.add(credit); | |
//EVENT LISTENERS FOR BUTTONS IN WIN1 | |
enablePush.addEventListener('click', function() { | |
if (!pushNotificationsEnabled) { | |
enablePushNotifications(); | |
} else { | |
disablePushNotifications(); | |
} | |
enablePush.title = pushNotificationsEnabled ? 'Enabled' : 'Disabled'; | |
}); | |
logoutButton.addEventListener('click', function(e) { | |
logout(); | |
}); | |
loginButton.addEventListener('click', function(e) { | |
win3.open(); | |
}); | |
createUserButton.addEventListener('click', function(e) { | |
win2.open(); | |
}); | |
subscribeButton.addEventListener('click', function(e) { | |
subscribe(); | |
}); | |
win1.add(scrollView) | |
win1.open(); | |
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | |
//CREATE USER WINDOW | |
var win2 = Titanium.UI.createWindow({ | |
title : 'Create User', | |
backgroundImage : 'pushexamplebg.png', | |
layout : 'vertical' | |
}); | |
var username = Ti.UI.createTextField({ | |
hintText : 'Username', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp', | |
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED, | |
autocapitalization : Ti.UI.TEXT_AUTOCAPITALIZATION_NONE, | |
autocorrect : false | |
}); | |
win2.add(username); | |
var password = Ti.UI.createTextField({ | |
hintText : 'Password', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp', | |
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED, | |
passwordMask : true | |
}); | |
win2.add(password); | |
var confirmPassword = Ti.UI.createTextField({ | |
hintText : 'Confirm Password', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp', | |
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED, | |
passwordMask : true | |
}); | |
win2.add(confirmPassword); | |
var fields = [username, password, confirmPassword] | |
var createSubmit = Ti.UI.createButton({ | |
title : 'Submit', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp' | |
}); | |
var backButton = Ti.UI.createButton({ | |
title : 'Back', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp' | |
}); | |
win2.add(createSubmit); | |
win2.add(backButton); | |
createSubmit.addEventListener('click', function(e) { | |
createUserSubmitForm(); | |
}); | |
backButton.addEventListener('click', function(e) { | |
win2.close(); | |
}); | |
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | |
//LOGIN WINDOW | |
var win3 = Titanium.UI.createWindow({ | |
title : 'Login', | |
backgroundImage : 'pushexamplebg.png', | |
layout : 'vertical' | |
}); | |
var loginUsername = Ti.UI.createTextField({ | |
hintText : 'Username', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp', | |
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED, | |
autocapitalization : Ti.UI.TEXT_AUTOCAPITALIZATION_NONE, | |
autocorrect : false | |
}); | |
win3.add(loginUsername); | |
var loginPassword = Ti.UI.createTextField({ | |
hintText : 'Password', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp', | |
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED, | |
passwordMask : true | |
}); | |
win3.add(loginPassword); | |
var loginSubmit = Ti.UI.createButton({ | |
title : 'Login Submit', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp' | |
}); | |
win3.add(loginSubmit); | |
var backButton1 = Ti.UI.createButton({ | |
title : 'Back', | |
top : '10dp', | |
left : '10dp', | |
right : '10dp', | |
height : '40dp' | |
}); | |
win3.add(backButton1); | |
loginSubmit.addEventListener('click', function(e) { | |
loginSubmitForm(); | |
}); | |
backButton1.addEventListener('click', function(e) { | |
win3.close(); | |
}); | |
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | |
//FUNCTIONS | |
function receivePush(e) { | |
alert('Received push: ' + JSON.stringify(e)); | |
} | |
function enablePushNotifications() { | |
pushNotificationsEnabled = true; | |
Ti.App.Properties.setBool('PushNotifications-Enabled', true); | |
checkPushNotifications(); | |
} | |
function disablePushNotifications() { | |
pushNotificationsEnabled = false; | |
Ti.App.Properties.setBool('PushNotifications-Enabled', false); | |
checkPushNotifications(); | |
} | |
function getAndroidPushModule() { | |
try { | |
return require('ti.cloudpush') | |
} catch (err) { | |
alert('Unable to require the ti.cloudpush module for Android!'); | |
pushNotificationsEnabled = false; | |
Ti.App.Properties.setBool('PushNotifications-Enabled', false); | |
return null; | |
} | |
} | |
function checkPushNotifications() { | |
if (pushNotificationsEnabled === null) { | |
pushNotificationsEnabled = Ti.App.Properties.getBool('PushNotifications-Enabled', false); | |
} | |
if (Ti.Platform.name === 'iPhone OS') { | |
if (pushNotificationsEnabled) { | |
if (Titanium.Platform.model == 'Simulator') { | |
alert('The simulator does not support push!'); | |
disablePushNotifications(); | |
return; | |
} | |
Ti.Network.registerForPushNotifications({ | |
types : [Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND], | |
success : deviceTokenSuccess, | |
error : deviceTokenError, | |
callback : receivePush | |
}); | |
} else { | |
Ti.Network.unregisterForPushNotifications(); | |
pushDeviceToken = null; | |
} | |
} else if (Ti.Platform.name === 'android') { | |
if (androidPushModule === null) { | |
androidPushModule = getAndroidPushModule(); | |
if (androidPushModule === null) { | |
return; | |
} | |
} | |
if (pushNotificationsEnabled) { | |
androidPushModule.enabled = true; | |
androidPushModule.addEventListener('callback', receivePush); | |
androidPushModule.retrieveDeviceToken({ | |
success : deviceTokenSuccess, | |
error : deviceTokenError | |
}); | |
} else { | |
androidPushModule.enabled = false; | |
androidPushModule.removeEventListener('callback', receivePush); | |
pushDeviceToken = null; | |
alert('Push notifications disabled'); | |
} | |
} | |
} | |
function deviceTokenSuccess(e) { | |
pushDeviceToken = e.deviceToken; | |
alert('Success! Registered for push. Now subscribe to any channel to recieve notifications'); | |
} | |
function deviceTokenError(e) { | |
alert('Failed to register for push! ' + e.error); | |
disablePushNotifications(); | |
} | |
function logout() { | |
Cloud.Users.logout(function(e) { | |
if (e.success) { | |
alert('Logged out!'); | |
} else { | |
status.text = (e.error && e.message) || e; | |
} | |
}); | |
} | |
function createUserSubmitForm() { | |
for (var i = 0; i < fields.length; i++) { | |
if (!fields[i].value.length) { | |
fields[i].focus(); | |
return; | |
} | |
fields[i].blur(); | |
} | |
if (password.value != confirmPassword.value) { | |
alert('Passwords do not match!'); | |
confirmPassword.focus(); | |
return; | |
} | |
//button.hide(); | |
Cloud.Users.create({ | |
username : username.value, | |
password : password.value, | |
password_confirmation : confirmPassword.value, | |
}, function(e) { | |
if (e.success) { | |
var user = e.users[0]; | |
alert('Created! You are now logged in as ' + user.id); | |
username.value = password.value = confirmPassword.value = ''; | |
win2.close(); | |
} else { | |
username.value = password.value = confirmPassword.value = ''; | |
alert(e); | |
} | |
// button.show(); | |
}); | |
} | |
function loginSubmitForm() { | |
Cloud.Users.login({ | |
login : loginUsername.value, | |
password : loginPassword.value | |
}, function(e) { | |
if (e.success) { | |
var user = e.users[0]; | |
loginUsername.value = loginPassword.value = ''; | |
alert('Logged in! You are now logged in as ' + user.id); | |
win3.close(); | |
} else { | |
alert(e); | |
} | |
//button.show(); | |
}); | |
} | |
function subscribe() { | |
// for (var i = 0; i < fields.length; i++) { | |
// if (!fields[i].value.length) { | |
// fields[i].focus(); | |
// return; | |
// } | |
// fields[i].blur(); | |
// } | |
// button.hide(); | |
Cloud.PushNotifications.subscribe({ | |
channel : channel.value, | |
device_token : pushDeviceToken, | |
type : Ti.Platform.name === 'iPhone OS' ? 'ios' : Ti.Platform.name | |
}, function(e) { | |
if (e.success) { | |
channel.value = ''; | |
alert('Subscribed!'); | |
} else { | |
alert(e); | |
} | |
//button.show(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment