Skip to content

Instantly share code, notes, and snippets.

@Amimul100
Forked from MotiurRahman/app.js
Created August 6, 2014 11:20
Show Gist options
  • Save Amimul100/3792d080f061ccf48c45 to your computer and use it in GitHub Desktop.
Save Amimul100/3792d080f061ccf48c45 to your computer and use it in GitHub Desktop.
/*Setting up an Android Application
First, add the CloudPush module to your project:
1.Open your project's tiapp.xml file.
2.In the Modules section, click the Add button (green plus sign).
3.Select ti.cloudpush and click OK .
In your project's JavaScript, require the module and initialize it with the CloudPush.retrieveDeviceToken()
method.On successful retrieval of the device token, set the CloudPush.enabled property to true to enable
the device to receive push notifications. You must call retrieveDeviceToken() before any other CloudPush
API, or else the device will not receive push notifications. Once push notifications are enabled, your
application can respond to CloudPush events (like the CloudPush.callback event) to process incoming push
notifications. For example:
Testing Environment:
1. Titanium SDK: 3.2.0.
2. Titanium CLI: 3.2.0,
3. Android SDK: 4.2.2
4. Win 7
Steps to Reproduce:
1. Create a classic Project.
2. Paste this code in app.js file and tiapp.xml file.
3. Enable cloud and add module cloud and cloudpush.
4. At Application Dashboard in setting option insert GCM API key and GCM Sender ID.
5. Create user in development option that you will use for login.
6. Run this code with testing environment.
*/
// Test Case
Titanium.UI.setBackgroundColor('#000');
var win = Ti.UI.createWindow({
backgroundColor : '#ccc',
title : 'Android Cloud Push Notification'
});
var CloudPush = require('ti.cloudpush');
CloudPush.debug = true;
CloudPush.enabled = true;
CloudPush.showTrayNotificationsWhenFocused = true;
CloudPush.focusAppOnPush = false;
var deviceToken;
var Cloud = require('ti.cloud');
Cloud.debug = true;
var submit = Ti.UI.createButton({
title : 'Register For Push Notification',
color : '#000',
height : 53,
width : 200,
top : 100,
});
win.add(submit);
submit.addEventListener('click', function(e) {
CloudPush.retrieveDeviceToken({
success : function deviceTokenSuccess(e) {
alert('Device Token: ' + e.deviceToken);
deviceToken = e.deviceToken;
loginDefault();
},
error : function deviceTokenError(e) {
alert('Failed to register for push! ' + e.error);
}
});
});
function loginDefault(e) {
// At first you need to create an user from the application dashboard
// Then login that email and password
Cloud.Users.login({
login : 'Email',
password : 'pass'
}, function(e) {
if (e.success) {
alert("login success");
defaultSubscribe();
} else {
alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
function defaultSubscribe() {
Cloud.PushNotifications.subscribe({
channel : 'alert',
device_token : deviceToken,
type : 'android'
}, function(e) {
if (e.success) {
alert('Subscribed for Push Notification!');
} else {
alert('Error:' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
CloudPush.addEventListener('callback', function(evt) {
//alert(evt);
//alert(evt.payload);
});
CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
Ti.API.info('Tray Click Launched App (app was not running)');
//alert('Tray Click Launched App (app was not running');
});
CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
Ti.API.info('Tray Click Focused App (app was already running)');
//alert('Tray Click Focused App (app was already running)');
});
win.open();
<property name="acs-push-type-development" type="string">gcm</property>
<property name="acs-push-type-production" type="string">gcm</property>
<property name="acs-push-type" type="string">gcm</property>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment