Appcelerator/ Titanium: Cannot Send Push Notification To Android
Solution 1:
Turns out my code was fine. The credentials I was using however was incorrect. Please see my other related question here:
Appcelerator/ Titanium: Getting Android credentials to push notifications
The docs are in need of updating.
Solution 2:
I'm hardly an expert with push, but I compared what you have to what I have in one of my apps.
Pretty sure you need to send the deviceToken into the subscribeToChannel function.
Try changing this -
function subscribeToChannel () {
to this -
functionsubscribeToChannel(deviceToken) {
then add the token to the call here -
subscribeToChannel (deviceToken);
Let me know if that works for you.
-Jon
Solution 3:
On subscribeToChannel()
function, you should use type : 'gcm'
instead of type: Ti.Platform.name
This is a commonJS module that I created for my Android push:
functionACSPush(_callback) {
var debug_mode = true;
varCloud = require('ti.cloud');
varCloudPush = require('ti.cloudpush');
CloudPush.enabled = true;
var deviceToken;
CloudPush.retrieveDeviceToken({
success : functiondeviceTokenSuccess(e) {
if(debug_mode)
Ti.API.info('Device Token: ' + e.deviceToken);
deviceToken = e.deviceToken;
if(Ti.App.Properties.getString("deviceToken") != deviceToken.toString()){
defaultSubscribe();
};
},
error : functiondeviceTokenError(e) {
if(debug_mode)
Ti.API.info('deviceTokenError.. :( ' + e.error);
}
});
functiondefaultSubscribe() {
Cloud.PushNotifications.subscribeToken({
channel : 'MyChannel',
device_token : deviceToken,
type : 'gcm'
}, function(e) {
if(e.success) {
if(debug_mode)
Ti.API.info("Success registerForPushNotifications");
Ti.App.Properties.setString("deviceToken", deviceToken.toString());
} else {
if(debug_mode)
Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
};
});
};
CloudPush.addEventListener('callback', function(evt) {
var payload = JSON.parse(evt.payload);
if(debug_mode){
Ti.API.info("Received a push notification\nPayload:\n" + JSON.stringify(evt.payload));
Ti.API.info("payload: " + payload);
};
_callback(payload);
});
CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
if(debug_mode)
Ti.API.info('Tray Click Launched App (app was not running)');
});
CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
if(debug_mode)
Ti.API.info('Tray Click Focused App (app was already running)');
});
};
module.exports = ACSPush;
Obviously, you must first configure Android Push Service http://docs.appcelerator.com/platform/latest/#!/guide/Configuring_push_services-section-src-37551713_Configuringpushservices-ConfiguringpushservicesforAndroiddevices
Post a Comment for "Appcelerator/ Titanium: Cannot Send Push Notification To Android"