Skip to content

Instantly share code, notes, and snippets.

@davidecassenti
Created June 28, 2013 17:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidecassenti/3ca943961098c0534e97 to your computer and use it in GitHub Desktop.
Save davidecassenti/3ca943961098c0534e97 to your computer and use it in GitHub Desktop.
Appcelerator ACS Push Notifications Example
(function() {
var platform = null;
var type = null;
var deviceToken = null;
var username = null;
var label = null;
// Cloud modules
var CloudPush = null; // required for Android
var Cloud = require('ti.cloud');
function init() {
platform = Ti.Platform.osname;
type = platform;
switch(platform) {
case "iphone":
case "ipad":
type = "ios";
buildUI();
break;
case "android":
CloudPush = require("ti.cloudpush");
CloudPush.debug = true;
buildUI();
break;
default:
showError("Platform not supported!");
}
}
function buildUI() {
var win = Ti.UI.createWindow({
backgroundColor : 'black',
layout: 'vertical'
});
var title = Ti.UI.createLabel({
text: "Appcelerator Cloud Services",
font: {
fontSize: "20dp"
},
color: "white",
top: "10dp",
height: Ti.UI.SIZE
})
win.add(title);
var btn = Ti.UI.createButton({
title: "Subscribe",
height: Ti.UI.SIZE,
top: '10dp'
});
btn.addEventListener('click', function(e) {
label.text = "";
// login the default user
checkUser();
});
win.add(btn);
label = Ti.UI.createLabel({
text: "",
width: Ti.UI.FILL,
height: Ti.UI.FILL,
color: "#dddddd",
top: '10dp'
});
win.add(label);
win.open();
}
function checkUser(retrying) {
Cloud.Users.logout(function (e) {
username = Ti.App.Properties.getString('username');
if(username) {
loginUser(retrying);
} else {
username = "";
for(var i=0; i<10; i++) {
username += ("" + Math.floor(10 * Math.random()));
}
addLog("Registering user " + username + "...");
Cloud.Users.create({
username: username,
password: 'logmein',
password_confirmation: 'logmein'
}, function (e) {
if (e.success) {
addLog("User registered.");
Ti.App.Properties.setString('username', username);
loginUser(retrying);
} else {
showError(e);
}
});
}
});
}
function loginUser(retrying) {
addLog("Logging in as " + username + "...");
Cloud.Users.login({
login : username,
password : 'logmein'
}, function(e) {
if (e.success) {
addLog("Logged in as " + username + ".");
getDeviceToken();
} else {
if(retrying) {
showError(e);
} else {
// try recreating the user
Ti.App.Properties.setString('username', null);
checkUser(true);
}
}
});
}
function getDeviceToken() {
switch(type) {
case "android":
CloudPush.retrieveDeviceToken({
success : registrationSuccess,
error : showError
});
break;
case "ios":
Titanium.Network.registerForPushNotifications({
types: [
Titanium.Network.NOTIFICATION_TYPE_ALERT
],
success: registrationSuccess,
error: showError,
callback: handleNotification
});
break;
}
}
function defaultUnsubscribe() {
Cloud.PushNotifications.unsubscribe({
device_token: deviceToken
}, function(e) {
defaultSubscribe();
});
}
function defaultSubscribe() {
Cloud.PushNotifications.subscribe({
channel: 'alert',
type: type,
device_token: deviceToken
},
function (e) {
if (e.success) {
addLog("Subscribed to the alert channel");
} else {
addLog("Already subscribed to the alert channel");
}
addLog("Ready to receive notifications");
});
switch(type) {
case "android":
CloudPush.addEventListener('callback', function(e) {
handleNotification(e);
});
}
}
function registrationSuccess(e) {
deviceToken = e.deviceToken;
addLog("Got device token: " + deviceToken);
if(platform == "android") {
CloudPush.enabled = true;
}
defaultUnsubscribe();
}
function showError(e) {
alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
}
function handleNotification(e) {
addLog("Notification received");
if(e && e.payload && e.payload.alert) {
alert(e.payload.alert);
} else {
alert(JSON.stringify(e));
}
}
function addLog(text) {
label.text = label.text + "\n" + text;
}
init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment