Skip to content

Instantly share code, notes, and snippets.

@lbrenman
Created September 13, 2014 20:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lbrenman/3168ebcefee22ee9d4e2 to your computer and use it in GitHub Desktop.
Save lbrenman/3168ebcefee22ee9d4e2 to your computer and use it in GitHub Desktop.
Appcelerator Titanium Hello Push with Rich Push
Basic Titanium Push Demo for iOS and Android
If you pass a URL in the push json payload in the htmlLink field, then the client will display the URL in a webview. This is Rich Push
function doClick(e) {
alert($.label.text);
Ti.Analytics.featureEvent('app.feature.labelClick', {});
}
var token;
$.index.open();
ACSLogin();
function clearToken(t){
var xhr = Titanium.Network.createHTTPClient({
onload: function() {
alert("reset_badge request success");
Ti.API.info("reset_badge request success");
Ti.Analytics.featureEvent('app.createHTTPClientOnload.reset_badge');
},
onerror: function(e) {
alert("reset_badge request onerror = "+e.error);
Ti.API.info("reset_badge request onerror = "+e.error);
Ti.Analytics.featureEvent('app.createHTTPClientError.reset_badge', {error: e.error});
},
timeout: 10000,
});
Ti.API.info("reset_badge - createHTTPClient - send()");
xhr.open("PUT", "https://api.cloud.appcelerator.com/v1/push_notification/reset_badge.json?key=643DyL4hgwiri4rdsvK475EnvbdWhryL");
xhr.send({
device_token: t
});
};
function ACSLogin(){
var Cloud = require('ti.cloud');
var userCredentials = {login : 'e', password: '1234'};
Cloud.Users.login(userCredentials, function(e) {
if (e.success) {
Ti.API.info("ACS: Log in to ACS successful");
Ti.API.info("ACS: Session ID = "+Cloud.sessionId);
Alloy.Globals.User = e.users[0];
Alloy.Globals.Session = Cloud.sessionId;
alert("ACS Login Successful, uid = "+Alloy.Globals.User.id);
if(Alloy.Globals.osname === 'android'){
registerForPush_Android();
} else {
registerForPush();
}
} else {
Ti.API.info('ACS: Login Error:' +((e.error && e.message) || JSON.stringify(e)));
alert("Error logging into ACS: "+((e.error && e.message) || JSON.stringify(e)));
Cloud.Users.create({
username : userCredentials.login,
password: userCredentials.password,
password_confirmation: userCredentials.password,
}, function(e){
if (e.success) {
Ti.API.info("New ACS userer creation successful");
alert("New user created");
if(Alloy.Globals.osname === 'android'){
registerForPush_Android();
} else {
registerForPush();
}
} else {
alert('User creation Error:' +((e.error && e.message) || JSON.stringify(e)));
}
});
}
});
};
function registerForPush_Android() {
Ti.API.info('registerForPush_Android()');
alert("registerForPush_Android");
if (Ti.Platform.model === 'Simulator' || Ti.Platform.model.indexOf('sdk') !== -1 ) {
Ti.API.info('This is a simulator, exit function registerForPush_Android()');
return;
}
var CloudPush = require('ti.cloudpush');
var Cloud = require('ti.cloud');
CloudPush.retrieveDeviceToken({
success: function deviceTokenSuccess(e) {
alert("registerForPush_Android - Device Token Received");
Ti.API.info('Device Token: ' + e.deviceToken);
token = e.deviceToken;
Cloud.PushNotifications.subscribe({
channel: 'broadcast',
//channel: 'suresh.thankappan@emc.com',
device_token: e.deviceToken,
type: 'gcm'
}, function (e) {
if (e.success) {
Ti.API.info('Successfully registered with channel: broadcast');
alert("Subscribed to push");
CloudPush.enabled = true;
} else {
alert('Error:\\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
},
error: function deviceTokenError(e) {
alert('Failed to register for push! ' + e.error);
},
callback: function(e){
/* CODE TO HANDLE INCOMING PUSH NOTIFICATIONS GOES HERE */
Ti.API.info('PushNotification::Data - ' +JSON.stringify(e.data));
alert("Push received = "+JSON.stringify(e.data));
if(e.data.htmlLink) {
var infoController = Alloy.createWidget('com.appcelerator.richpushscreen', { link: e.data.htmlLink });
var infoControllerWin = infoController.getView();
infoControllerWin.open();
}
}
});
}
function registerForPush() {
Ti.API.info('registerForPush()');
if (Ti.Platform.model === 'Simulator' || Ti.Platform.model.indexOf('sdk') !== -1 ) {
Ti.API.info('This is a simulator, exit function registerForPush()');
return;
}
Ti.Network.registerForPushNotifications({
success: function(e){
Ti.API.info('PushNotification::DeviceToken: '+e.deviceToken);
token = e.deviceToken;
var Cloud = require('ti.cloud');
Cloud.PushNotifications.subscribe({
channel: 'broadcast',
//channel: 'suresh.thankappan@emc.com',
device_token: e.deviceToken,
type: 'ios'
}, function (e) {
if (e.success) {
Ti.API.info('Successfully registered with channel: broadcast');
alert("Subscribed to push");
} else {
alert('Error:\\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
},
error: function(e){
alert('Failed to Register device for notifications - ' + e.error);
},
callback: function(e){
/* CODE TO HANDLE INCOMING PUSH NOTIFICATIONS GOES HERE */
Ti.API.info('PushNotification::Data - ' +JSON.stringify(e.data));
//Ti.UI.iPhone.appBadge+=1;
Ti.UI.iPhone.appBadge=null;
//Currently not supported
// Cloud.PushNotifications.reset_badge({
// device_token: token
// });
clearToken(token);
alert("Push received = "+JSON.stringify(e.data));
if(e.data.htmlLink) {
var infoController = Alloy.createWidget('com.appcelerator.richpushscreen', { link: e.data.htmlLink });
var infoControllerWin = infoController.getView();
infoControllerWin.open();
}
},
types: [
Titanium.Network.NOTIFICATION_TYPE_BADGE,
Titanium.Network.NOTIFICATION_TYPE_ALERT,
Titanium.Network.NOTIFICATION_TYPE_SOUND
]});
};
if (Ti.Platform.osname === 'iphone' || Ti.Platform.osname === 'ipad')
{
var touchTestModule = undefined;
try
{
touchTestModule = require("com.soasta.touchtest");
}
catch (tt_exception)
{
Ti.API.error("com.soasta.touchest module is required");
}
var cloudTestURL = Ti.App.getArguments().url;
if (cloudTestURL != null)
{
// The URL will be null if we don't launch through TouchTest.
touchTestModule && touchTestModule.initTouchTest(cloudTestURL);
}
Ti.App.addEventListener('resumed',function(e)
{
// Hook the resumed from background
var cloudTestURL = Ti.App.getArguments().url;
if (cloudTestURL != null)
{
touchTestModule && touchTestModule.initTouchTest(cloudTestURL);
}
});
}
".container": {
backgroundColor:"white"
},
"Label": {
width: Ti.UI.SIZE,
height: Ti.UI.SIZE,
color: "#000"
}
<?xml version="1.0" encoding="UTF-8"?>
<ti:app xmlns:ti="http://ti.appcelerator.org">
<property name="com-appcelerator-apm-id" type="string"><YOUR KEY></property>
<property name="acs-authbase-url" type="string">https://secure-identity.cloud.appcelerator.com</property>
<property name="acs-base-url" type="string">https://api.cloud.appcelerator.com</property>
<property name="acs-oauth-secret-development" type="string"><YOUR KEY></property>
<property name="acs-oauth-key-development" type="string"><YOUR KEY></property>
<property name="acs-api-key-development" type="string"><YOUR KEY></property>
<property name="acs-oauth-secret-production" type="string"><YOUR KEY></property>
<property name="acs-oauth-key-production" type="string"><YOUR KEY></property>
<property name="acs-api-key-production" type="string"><YOUR KEY></property>
<id>com.leorbrenman.hellopush</id>
<name>hellopush</name>
<version>1.0</version>
<publisher>lbrenman</publisher>
<url>http://</url>
<description>not specified</description>
<copyright>2013 by lbrenman</copyright>
<icon>appicon.png</icon>
<persistent-wifi>false</persistent-wifi>
<prerendered-icon>false</prerendered-icon>
<statusbar-style>default</statusbar-style>
<statusbar-hidden>false</statusbar-hidden>
<fullscreen>false</fullscreen>
<navbar-hidden>false</navbar-hidden>
<analytics>true</analytics>
<guid><YOUR GUID></guid>
<property name="ti.ui.defaultunit" type="string">system</property>
<iphone>
<orientations device="iphone">
<orientation>Ti.UI.PORTRAIT</orientation>
</orientations>
<orientations device="ipad">
<orientation>Ti.UI.PORTRAIT</orientation>
<orientation>Ti.UI.UPSIDE_PORTRAIT</orientation>
<orientation>Ti.UI.LANDSCAPE_LEFT</orientation>
<orientation>Ti.UI.LANDSCAPE_RIGHT</orientation>
</orientations>
</iphone>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application>
<receiver android:name="com.appcelerator.cloud.push.PushBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="com.appcelerator.cloud.push.PushService.MSG_ARRIVAL"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
<meta-data
android:name="com.appcelerator.cloud.push.BroadcastReceiver.ArrivalActivity" android:value="com.cocoafish.pushnotifications.ArrivalActivity"/>
</receiver>
<service android:name="com.appcelerator.cloud.push.PushService"/>
<activity
android:configChanges="keyboardHidden|orientation"
android:label="hellopush"
android:name=".HellopushActivity" android:theme="@style/Theme.Titanium">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<data android:scheme="touchtest-5d26463c-2618-4346-a83e-a5a2c11c650b"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<service android:enabled="true" android:exported="false" android:name="com.soasta.android.touchtest.TouchTestService"/>
</application>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
</manifest>
</android>
<mobileweb>
<precache/>
<splash>
<enabled>true</enabled>
<inline-css-images>true</inline-css-images>
</splash>
<theme>default</theme>
</mobileweb>
<modules>
<module platform="android">com.appcelerator.apm</module>
<module deploy-type="test" platform="iphone" version="1.0">com.soasta.touchtest</module>
<module platform="android">ti.cloudpush</module>
<module platform="commonjs">ti.cloud</module>
<module deploy-type="development" platform="android" version="1.0">com.soasta.touchtest</module>
<module deploy-type="development" platform="iphone" version="1.0">com.soasta.touchtest</module>
<module platform="iphone">com.appcelerator.apm</module>
<module deploy-type="test" platform="android" version="1.0">com.soasta.touchtest</module>
</modules>
<deployment-targets>
<target device="android">true</target>
<target device="blackberry">false</target>
<target device="ipad">true</target>
<target device="iphone">true</target>
<target device="mobileweb">false</target>
<target device="tizen">false</target>
</deployment-targets>
<sdk-version>3.2.0.GA</sdk-version>
<plugins>
<plugin version="1.0">ti.alloy</plugin>
<plugin>com.soasta.touchtest.android</plugin>
</plugins>
<property name="com-soasta-touchtest-version" type="string">6656.155</property>
<ios>
<plist>
<dict>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.leorbrenman.hellopush</string>
<key>CFBundleURLSchemes</key>
<array>
<string>touchtest-5d26463c-2618-4346-a83e-a5a2c11c650b</string>
</array>
</dict>
</array>
</dict>
</plist>
</ios>
<property name="com-soasta-touchtest-ios-appId" type="string">168039</property>
</ti:app>
var _args = arguments[0] || {};
$.richPushWebView.url = _args.link;
$.closeBtn.addEventListener("click", function(e) {
Ti.API.info("richPush: Close Button click event");
Ti.Analytics.featureEvent('app.feature.richPushScreen.closeBtnclick', {});
Alloy.Globals.apm && Alloy.Globals.apm.leaveBreadcrumb('app.richPushScreen.feature.closeBtnclick');
$.richPush.close();
});
"Window": {
backgroundColor: 'transparent',
orientationModes: [Ti.UI.PORTRAIT],
fullscreen: true,
navBarHidden: true
}
".titleBarView":{
backgroundColor: 'gray',
backgroundGradient:{
type:'linear',
colors:[{color: '#414141', offset: 0.0},{color: '#222222', offset: 0.5},{color: '#141414', offset: 0.5},{color: '#141414', offset: 1.0}],
startPoint:{x:"0%",y:"0%"},
endPoint:{x:"0%",y:"100%"},
backFillStart:true
},
height:'44dp',
top:'0'
}
".titleBarLbl": {
font: {fontFamily:'Helvetica Neue', fontSize:"18dp", fontWeight:'bold'},
textAlign:Ti.UI.TEXT_ALIGNMENT_CENTER,
wordWrap:false,
color:'#fff'
}
"#closeBtn":{
width: '32dp',
height: '32dp',
right:'5dp',
image: WPATH('images/closeBtn.png')
}
<Alloy>
<Window id="richPush" modal="true">
<View class='container' layout='vertical'>
<View class='titleBarView'>
<ImageView id='closeBtn' touchTestId='richPushCloseBtnTTID' />
<Label id="richPushTitleBarLbl" class='titleBarLbl' touchTestId='richPushTitleBarLblTTID'>Rich Push</Label>
</View>
<!-- <WebView height="Ti.UI.FILL" id="richPushWebView" url="http://www.appcelerator.com"></WebView> -->
<WebView height="Ti.UI.FILL" id="richPushWebView"></WebView>
</View>
</Window>
</Alloy>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment