Skip to content

Instantly share code, notes, and snippets.

@rakhimoni
Last active October 1, 2015 11:15
Show Gist options
  • Save rakhimoni/639b18c5b5b04e2a8a99 to your computer and use it in GitHub Desktop.
Save rakhimoni/639b18c5b5b04e2a8a99 to your computer and use it in GitHub Desktop.
Android Push Notification
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.showTrayNotification = 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();
});
var submit1 = Ti.UI.createButton({
title : 'Subscribe Device',
color : '#000',
height : 60,
width : 200,
top : 200,
});
win.add(submit1);
submit1.addEventListener('click', function(e) {
defaultSubscribe();
});
var submit2 = Ti.UI.createButton({
title : 'Unsubscribe Device',
color : '#000',
height : 60,
width : 200,
top : 300,
});
win.add(submit2);
submit2.addEventListener('click', function(e) {
defaultUnSubscribe();
});
CloudPush.retrieveDeviceToken({
success : deviceTokenSuccess,
error : deviceTokenError
});
function deviceTokenSuccess(e) {
deviceToken = e.deviceToken;
}
function deviceTokenError(e) {
alert('Failed to register for push notifications! ' + e.error);
}
CloudPush.addEventListener('callback', function(evt) {
alert("Notification received: " + evt.payload);
});
CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
Ti.API.info('Tray Click Launched App (app was not running)');
});
CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
Ti.API.info('Tray Click Focused App (app was already running)');
});
function loginDefault(e) {
Cloud.Users.login({
login : 'rmonicse@gmail.com',
password : '123456'
}, function(e) {
if (e.success) {
alert("Successfully Loged In");
} 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('Device Subscribed!');
} else {
alert('Error:' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
//APA91bGjGnm0VBXlhXKyUkYj0czk2APRlUydg_n72GscbZ5P-OjeZ8m3uJzCdqEBtnAhnlw1OV5JycHPN10EWKw1fEg7jd1CM6zYFgROIYxcGLoQdT0mAN7_NE61aQQuP-Kjii5d9ZZF
function defaultUnSubscribe() {
Cloud.PushNotifications.unsubscribe({
channel: 'alert',
device_token: deviceToken,
type : 'android'
}, function (e) {
if (e.success) {
alert('Device Unsubscribed!');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
Cloud.PushNotifications.notifyTokens({
channel: 'alert',
to_tokens:'APA91bGjGnm0VBXlhXKyUkYj0czk2APRlUydg_n72GscbZ5P-OjeZ8m3uJzCdqEBtnAhnlw1OV5JycHPN10EWKw1fEg7jd1CM6zYFgROIYxcGLoQdT0mAN7_NE61aQQuP-Kjii5d9ZZF' ,
payload: 'Welcome to push notifications rakhi'
}, function (e) {
if (e.success) {
alert('Success');
} else {
alert('Error:\n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
win.open();
[INFO] : {"success":true,"error":false,"meta":{"status":"ok","code":200,"method_name":"NotifyTokens"}}
[INFO] : ALERT: (KrollRuntimeThread) [3553,3929] Success
[INFO] : AppCompatDelegate: The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's
[INFO] : APSCloudPush: receivePayload: {"android":{"alert":"Welcome to push notifications rakhi"}}
[INFO] : APSCloudPush: background: true
[INFO] : APSCloudPush: queuePayload: {"android":{"alert":"Welcome to push notifications rakhi"}}
[INFO] : APSCloudPush: showTrayNotification
[INFO] : APSCloudPush: getTrayPendingIntent
[INFO] : APSCloudPush: getFocusIntent payload: {"android":{"alert":"Welcome to push notifications rakhi"}}
[INFO] : APSCloudPush: processCallbackIfInstantiated payload: {"android":{"alert":"Welcome to push notifications rakhi"}}
[INFO] : APSCloudPush: processQueuedCallback
[INFO] : ALERT: (KrollRuntimeThread) [3759,7688] Notification received: {"android":{"alert":"Welcome to push notifications rakhi”}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment