Skip to content

Instantly share code, notes, and snippets.

@MotiurRahman
Last active August 29, 2015 13:55
Show Gist options
  • Save MotiurRahman/8766037 to your computer and use it in GitHub Desktop.
Save MotiurRahman/8766037 to your computer and use it in GitHub Desktop.
Android: ACS push notification sample code
/*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.3.0.
2. Titanium CLI: 3.3.0,
3. Android SDK: 4.4.2,4.4.2
Steps to Reproduce:
1. Create a classic Project.
2. Paste this code in app.js 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 a user in development option that you will use for login.
6. Run this code with the testing environment.
*/
// Test Case
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=null;
var Cloud = require('ti.cloud');
Cloud.debug = true;
var submit = Ti.UI.createButton({
title : 'Register For Push Notification',
color : '#000',
height : 60,
width : 200,
top : 100,
});
win.add(submit);
submit.addEventListener('click', function(e) {
loginDefault();
});
CloudPush.retrieveDeviceToken({
success : deviceTokenSuccess,
error : deviceTokenError
});
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
deviceToken = e.deviceToken;
}
function deviceTokenError(e) {
alert('Failed to register for push notifications! ' + e.error);
}
// Process incoming push notifications
CloudPush.addEventListener('callback', function(evt) {
alert("Notification received: " + evt.payload);
});
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)));
}
});
}
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment