Created
August 30, 2012 14:54
-
-
Save mschmulen/3530106 to your computer and use it in GitHub Desktop.
Titanium ACS Notifications
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
var UIWinNotifications = (function() { | |
var CLOUD = require('ti.cloud'); | |
CLOUD.debug = true; | |
var API = {}; | |
API.APP = null; | |
API.name = "Notifications"; | |
API.icon = "/KS_nav_views.png"; | |
API.win = null; | |
API.pushNotificationsEnabled = Ti.App.Properties.setBool('PushNotifications-Enabled', true); | |
API.androidPushModule = null; | |
API.pushDeviceToken = null; | |
API.pushNotificationsEnabled = null; | |
API.initialize = function(args) { | |
Ti.API.info( 'UIWinNotifications initialize' ); | |
API.checkPushNotifications(); | |
}//end API.initialize | |
API.receivePush = function(e) { | |
Ti.API.info("API.receivePush" + JSON.stringify(e) ); | |
alert('API.receivePush push:' + JSON.stringify(e) ); | |
}//end receivePush | |
API.getAndroidPushModule = function(args) { | |
try { | |
return require('ti.cloudpush') | |
} | |
catch (err) { | |
//alert('Unable to require the ti.cloudpush module for Android!'); | |
API.pushNotificationsEnabled = false; | |
Ti.App.Properties.setBool('PushNotifications-Enabled', false); | |
return null; | |
} | |
}//end API.getAndroidPushModule | |
API.checkPushNotifications = function(args) { | |
if (API.pushNotificationsEnabled === null) { | |
API.pushNotificationsEnabled = Ti.App.Properties.getBool('PushNotifications-Enabled', true); | |
} | |
if (Ti.Platform.name === 'iPhone OS') { | |
//ENABLE PUSH NOTIFICATIONS | |
if (API.pushNotificationsEnabled) { | |
if (Titanium.Platform.model == 'Simulator') { | |
alert('The simulator does not support push!'); | |
API.disablePushNotifications(); | |
return; | |
}//end if | |
Ti.Network.registerForPushNotifications({ | |
types: [ | |
Ti.Network.NOTIFICATION_TYPE_BADGE, | |
Ti.Network.NOTIFICATION_TYPE_ALERT, | |
Ti.Network.NOTIFICATION_TYPE_SOUND | |
], | |
success: API.deviceTokenSuccess, | |
error: API.deviceTokenError, | |
callback: API.receivePush | |
});//end registerForPushNotifications | |
}//end if | |
else { | |
alert(" Unregister for Push !!"); | |
//Ti.Network.unregisterForPushNotifications(); | |
//pushDeviceToken = null; | |
}//end else | |
}//end if | |
else if (Ti.Platform.name === 'android') { | |
/* | |
if (API.androidPushModule === null) { | |
API.androidPushModule = getAndroidPushModule(); | |
if (API.androidPushModule === null) { | |
return; | |
} | |
} | |
if (pushNotificationsEnabled) { | |
API.androidPushModule.enabled = true; | |
API.androidPushModule.addEventListener('callback', API.receivePush); | |
API.androidPushModule.retrieveDeviceToken({ | |
success: API.deviceTokenSuccess, | |
error: API.deviceTokenError | |
}); | |
} | |
else { | |
API.androidPushModule.enabled = false; | |
API.androidPushModule.removeEventListener('callback', API.receivePush); | |
API.pushDeviceToken = null; | |
}//end else | |
*/ | |
} | |
}//end API.checkPushNotifications | |
API.deviceTokenSuccess = function(e) { | |
if ( API.pushDeviceToken == null ) | |
{ | |
API.pushDeviceToken = e.deviceToken; | |
Ti.App.Properties.setString("device_token",API.pushDeviceToken); | |
var user_device_token = Ti.App.Properties.getString("device_token",null); | |
CLOUD.PushNotifications.subscribe({ | |
channel: 'yack', | |
type:'ios', | |
device_token: user_device_token | |
}, function (e) { | |
if (e.success) { | |
alert('Success'+((e.error && e.message) || JSON.stringify(e))); | |
} else { | |
alert('Error:\\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
}//end if | |
}//end deviceTokenSuccess | |
API.deviceTokenError = function(e) { | |
alert('Failed to register for push! ' + e.error); | |
API.disablePushNotifications(); | |
}//end deviceTokenError | |
API.enablePushNotifications = function(args) { | |
API.pushNotificationsEnabled = true; | |
Ti.App.Properties.setBool('PushNotifications-Enabled', true); | |
API.checkPushNotifications(); | |
}//enablePushNotifications | |
API.disablePushNotifications = function(args) { | |
API.pushNotificationsEnabled = false; | |
Ti.App.Properties.setBool('PushNotifications-Enabled', false); | |
API.checkPushNotifications(); | |
}//enablePushNotifications | |
/* | |
CLOUD.PushNotifications.notify(data, function (e) { | |
if (e.success) { | |
alert('Notified!'); | |
} | |
else { | |
error(e); | |
} | |
button.show(); | |
}); | |
} | |
button.addEventListener('click', submitForm); | |
for (var i = 0; i < fields.length; i++) { | |
fields[i].addEventListener('return', submitForm); | |
} | |
win.addEventListener('open', function () { | |
channel.focus(); | |
}); | |
*/ | |
return API; | |
})(); //end UIWinNotifications | |
module.exports = UIWinNotifications; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment