Created
December 29, 2013 16:04
-
-
Save csemrm/8171882 to your computer and use it in GitHub Desktop.
Android Push notification using device Tokenin Titanium
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 currentUser = null; | |
var loggedIn = false; | |
var Cloud = require('ti.cloud'); | |
var categories = []; | |
var event_types = []; | |
Cloud.debug = true; | |
function subscribeToChannel(deviceToken) { | |
Cloud.PushNotifications.subscribeToken({ | |
device_token : deviceToken, | |
channel : 'JonoPriyo', | |
type : Ti.Platform.name == 'android' ? 'android' : 'ios' | |
}, function(e) { | |
if (e.success) { | |
Ti.API.info('Subscribed'); | |
} else { | |
Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
}; | |
exports.subscribeToChannel = subscribeToChannel; | |
exports.sendTestNotification = function(deviceToken) { | |
Cloud.PushNotifications.notifyTokens({ | |
to_tokens : deviceToken, | |
channel : 'test', | |
payload : 'This is a test.' | |
}, function(e) { | |
if (e.success) { | |
Ti.API.info('Push notification sent'); | |
} else { | |
Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
}; | |
exports.unsubscribeToChannel = function(deviceToken) { | |
Cloud.PushNotifications.unsubscribeToken({ | |
device_token : deviceToken, | |
channel : 'JonoPriyo', | |
}, function(e) { | |
subscribeToChannel(deviceToken); | |
}); | |
}; |
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
Titanium.UI.setBackgroundColor('#fff'); | |
var win = Ti.UI.createWindow(); | |
var deviceToken = null; | |
var CloudPush = null; | |
var isIos = (Ti.Platform.osname === 'iphone' || Ti.Platform.osname === 'ipad'); | |
var acs = require('acs'); | |
CloudPush = require('ti.cloudpush'); | |
CloudPush.retrieveDeviceToken({ | |
success : deviceTokenSuccess, | |
error : deviceTokenError | |
}); | |
// Process incoming push notifications | |
CloudPush.addEventListener('callback', function(evt) { | |
Ti.API.info(evt.payload); | |
}); | |
// Triggered when the push notifications is in the tray when the app is not running | |
CloudPush.addEventListener('trayClickLaunchedApp', function(evt) { | |
Ti.API.info('Tray Click Launched App (app was not running)'); | |
}); | |
// Triggered when the push notifications is in the tray when the app is running | |
CloudPush.addEventListener('trayClickFocusedApp', function(evt) { | |
Ti.API.info('Tray Click Focused App (app was already running)'); | |
}); | |
function deviceTokenError(e) { | |
Ti.API.info('Failed to register for push notifications! ' + e.error); | |
} | |
// Save the device token for subsequent API calls | |
function deviceTokenSuccess(e) { | |
deviceToken = e.deviceToken; | |
Ti.API.info('Sucess ' + e.deviceToken); | |
if (!isIos) { | |
CloudPush.enabled = true; | |
} | |
acs.unsubscribeToChannel(deviceToken); | |
} | |
win.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment