Registration id is giving but event is not triggered. I don't understand why?
Please Help me. I did not understand where wrong?
// Cloud module and Cloud module use
Titanium.UI.setBackgroundColor('#000');
var win = Ti.UI.createWindow({
backgroundColor : '#ccc',
title : 'Android Cloud Push Notification'
});
//CloudPush modules variables
var CloudPush = require('ti.cloudpush');
CloudPush.debug = true;
CloudPush.enabled = true;
CloudPush.showTrayNotification = true;
CloudPush.showTrayNotificationsWhenFocused = true;
CloudPush.focusAppOnPush = false;
//Device Token variables
var deviceToken;
var Cloud = require('ti.cloud');
Cloud.debug = true;
//create button
var submit = Ti.UI.createButton({
title : 'Register For Push Notification',
color : '#000',
height : 53,
width : 200,
top : 100,
});
//add button on win scene
win.add(submit);
//CloudPush retrieveDeviceToken function
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);
}
});
});
//Cloud login
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 : '===',
password : '==='
}, 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)));
}
});
}
//Doesnt Work this events
CloudPush.addEventListener('callback', function(evt) {
console.log(evt);
console.log(evt.payload);
});
//Doesnt Work this events
CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
console.log('Tray Click Launched App (app was not running)');
//alert('Tray Click Launched App (app was not running');
});
//Doesnt Work trayClickFocusedApp events
CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
console.log('Tray Click Focused App (app was already running)');
//alert('Tray Click Focused App (app was already running)');
});
win.open();
Related
I want to be able to send push notifications using Titanium and Arrow Push on Android.
I have followed the instructions here:
Configuring Push Services
Subscribing to push notifications
Modules.CloudPush
My simple code looks as follows:
var CloudPush = require('ti.cloudpush');
var deviceToken = null;
// Works fine
CloudPush.retrieveDeviceToken({
success: function () {
deviceToken = e.deviceToken;
alert('deviceToken: ' + deviceToken);
subscribeToChannel();
},
error: function () {
alert('Failed to register for push notifications! ' + e.error);
}
});
// Never runs!!!
CloudPush.addEventListener('callback', function (evt) {
Ti.API.info('New notification!');
alert("Notification received: " + evt.payload);
});
// Works fine
function subscribeToChannel () {
Cloud.PushNotifications.subscribeToken({
device_token: deviceToken,
channel: 'general',
type: Ti.Platform.name
}, function (e) {
if (e.success) {
alert('Subscribed');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
Most of the above code is similar to the docs. The subscription aspect of the code seems to works perfectly fine, as the user's device also appears in the devices section of the Appcelerator Dashboard.
However when it comes to sending a notification, from the Appcelerator Dashboard, the word "Failure" appears next to my Android device.
The full error message when highlighting the "?" icon is as follows:
Exception Type: GCM; Error Code: 3103; Error Message:
RegistrationId(s) is null or empty; Catched Exception: argument cannot
be null
I looked this error up on http://docs.appcelerator.com/arrowdb/latest/#!/guide/troubleshooting and all it says is:
The GCM client provided a null or empty registration ID. This error is
uncommon if you are using the Modules.CloudPush module.
Which isn't helpful.
What am I doing wrong? Is this a bug on Accelerator side.
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.
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 -
function subscribeToChannel (deviceToken) {
then add the token to the call here -
subscribeToChannel (deviceToken);
Let me know if that works for you.
-Jon
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:
function ACSPush(_callback) {
var debug_mode = true;
var Cloud = require('ti.cloud');
var CloudPush = require('ti.cloudpush');
CloudPush.enabled = true;
var deviceToken;
CloudPush.retrieveDeviceToken({
success : function deviceTokenSuccess(e) {
if(debug_mode)
Ti.API.info('Device Token: ' + e.deviceToken);
deviceToken = e.deviceToken;
if(Ti.App.Properties.getString("deviceToken") != deviceToken.toString()){
defaultSubscribe();
};
},
error : function deviceTokenError(e) {
if(debug_mode)
Ti.API.info('deviceTokenError.. :( ' + e.error);
}
});
function defaultSubscribe() {
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
I need to make a push notification listener, My code for this is below and it runs perfect till the subscription and the dashboard confirms it is subscribed successfully, but the problem occurs with the receiver.
Alloy.Globals.ServiceAddress = "my service address";
Ti.API.info("1");
var CloudPush = require('ti.cloudpush');
var Cloud = require("ti.cloud");
var deviceToken = null;
loginUser(); // flow starts from here
function loginUser() {
Cloud.Users.login({
login: 'User',
password: 'Password'
}, function(e) {
if (e.success) {
var user = e.users[0];
// alert("Loggin successfully");
getDeviceToken();
} else {
alert("Error2*** :" + e.message);
//Ti.API.info('error ')
}
});
}
function getDeviceToken() {
if (Ti.Platform.Android) {
CloudPush.debug = true;
CloudPush.focusAppOnPush = false;
CloudPush.enabled = true;
CloudPush.showAppOnTrayClick = true;
CloudPush.showTrayNotification = true;
CloudPush.singleCallback = true;
CloudPush.singleCallback = true;
CloudPush.retrieveDeviceToken({
success: deviceTokenSuccess,
error: deviceTokenError
});
}
}
// Enable push notifications for this device
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
deviceToken = e.deviceToken;
alert(e.deviceToken);
subscribeToChannel(deviceToken);
}
function deviceTokenError(e) {
alert('Failed to register for push notifications! ' + e.error);
}
// Process incoming push notifications
function subscribeToChannel(deviceToken) {
// Subscribes the device to the 'news_alerts' channel
// Specify the push type as either 'android' for Android or 'ios' for iOS
Cloud.PushNotifications.subscribeToken({
device_token: deviceToken,
channel: 'Vision', //'You_App',
type: Ti.Platform.name == 'android' ? 'android' : 'ios'
}, function(e) {
if (e.success) {
alert('Subscribed');
SendNotification(deviceToken);
} else {
// indicator.visible = false;
alert('Subscribe Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
function SendNotification(to_deviceToken) {
alert('sendnot');
Cloud.PushNotifications.notifyTokens({
channel: 'My Cannel',
to_tokens: to_deviceToken,
payload: 'Welcome to push notifications'
}, function(e) {
if (e.success) {
alert('Success Send');
} else {
alert('Send Error:\n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
}
When I add event-listener to push notifications it doesn't even fire up however, the client is saying it is connected.
<!-- language: lang-js -->
CloudPush.addEventListener('callback', function (evt) {
alert('rec');
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)');
});
PS. I have searched and already read related threads Like this one
I am attaching the log to make it clarify that subscription and notification send is working properly
Push notification Log
and
Connected Clients
Solved
I was compiling with titanium api level 4.1.1.v20151203143424 with CloudPush module version 3.2.1
I migrated to titanium 6.0.0.v20160202173231 with cloudpush module version 3.4.1
and all set see in this pic
I added push notification "events" but is not working.
I dont understand. Error can not be given and Log in is not working.
please help me. Thanks
my gcm module
var gcm = require("nl.vanvianen.android.gcm");
var CloudPush = require('ti.cloudpush');
/* If the app is started or resumed act on pending data saved when the notification was received */
var lastData = gcm.getLastData();
if (lastData) {
Ti.API.info("Last notification received " + JSON.stringify(lastData));
gcm.clearLastData();
}
gcm.registerPush({
senderId : '480608162759',
notificationSettings : {
sound : 'mysound.mp3',
smallIcon : 'notification_icon.png',
largeIcon : 'appicon.png',
vibrate : true
},
success : function(event) {
Ti.API.info("Push registration success: " + JSON.stringify(event));
},
error : function(event) {
Ti.API.info("Push registration error = " + JSON.stringify(event));
alert(event.error);
},
callback : function(event) {
Ti.API.info("Push callback = " + JSON.stringify(event));
var dialog = Ti.UI.createAlertDialog({
title : 'Push received',
message : JSON.stringify(event.data)
// buttonNames: ['View'],
// cancel: 1
});
dialog.addEventListener("click", function(event) {
dialog.hide();
if (event.index == 0) {
/* Do stuff to view the notification */
}
});
dialog.show();
},
});
//Dont work eventlistener
CloudPush.addEventListener('callback', function(evt) {
console.log("Notification received: " + evt.payload);
});
I am building a mobile application for both android/Ios,For android,require a push notification using parse to deliver my message and to get my request. I got the device token for my android device and i can list my recipients but if i click on send button my message is not delivered.Here is my code that i have
var uuid=Ti.Platform.createUUID();
var deviceToken;
var CloudPush = require('ti.cloudpush');
//fetch device token
CloudPush.retrieveDeviceToken({
success : function deviceTokenSuccess(e) {
deviceToken = e.deviceToken;
//alert('Device Token: ' + deviceToken);
Ti.API.info('Device Token: ' + e.deviceToken);
loginDefault();
/////////parse start///
var c = Titanium.Network.createHTTPClient();
c.setTimeout(25000);
c.onload = function(e) {
Ti.API.info("onload");
svar = JSON.parse(this.responseText);
Ti.API.info(svar);
Ti.API.info('params'+this.responseText);
};
c.onerror = function(e) {
Ti.API.info("on error");
alert(e);
};
c.open('POST', 'https://api.parse.com/1/installations');
c.setRequestHeader('X-Parse-Application-Id', 'bopIfF9m4JpkAxww9syYvLHVaCmE2go9WW7uHS1K');
c.setRequestHeader('X-Parse-REST-API-Key', 'NZLlV86V8ruxq5GdXRi2NrepQXhyiSmmoDHeZasH');
c.setRequestHeader('Content-Type', "application/json; charset=utf-8");
var params = {
"deviceType": "android",
"deviceToken": deviceToken,
"installationId":uuid,
"pushType":"gcm",
};
Ti.API.info('value is'+params);
c.send(JSON.stringify(params));
///parse end/////
},
error : function deviceTokenError(e) {
// alert('Failed to register for push! ' + e.error);
}
});
CloudPush.debug = true;
CloudPush.enabled = true;
CloudPush.showTrayNotificationsWhenFocused = true;
CloudPush.focusAppOnPush = false;
var Cloud = require('ti.cloud');
Cloud.debug = true;
function loginDefault(e) {
//Create a Default User in Cloud Console, and login with same credential
Cloud.Users.login({
login : 'push1',
password : '12345'
},
function(e) {
if (e.success) {
// alert("Login success");
defaultSubscribe();
} else {
// alert('Login error: ' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
function defaultSubscribe() {
Cloud.PushNotifications.subscribe({
channel : 'alert',//'alert' is channel name
device_token : deviceToken,
type : 'gcm' //here i am using gcm, it is recomended one
}, function(e) {
if (e.success) {
// alert('Subscribed for Push Notification!');
} else {
//alert('Subscrib error:' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
CloudPush.addEventListener('callback', function(evt) {
// alert(evt.payload);
// alert("ggg");
});
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)');
});
Change
c.setRequestHeader('Content-Type', "application/json; charset=utf-8");
to
c.setRequestHeader('Content-Type', "application/json");
I am trying to show push notifications in android app, developed by titanium studio, my getting alert with unfortunately app is closed after this my is clossing, my code is
try {
CloudPush.retrieveDeviceToken({
success : function deviceTokenSuccess(e) {
deviceID = e.deviceToken;
Ti.API.info(deviceID);
Ti.App.Properties.setString('deviceid', '' + deviceID);
},
error : function deviceTokenError(e) {
alert('Failed to register for push! ' + e.error);
}
});
} catch(e) {
alert('Error :', e);
}
// Enable push notifications for this device
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
CloudPush.enabled = true;
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(evt.payload);
var alertNotification = Titanium.UI.createAlertDialog({
title : 'app',
message : evt.data.alert,
cancel : 1,
buttonNames : ['OK']
});
alertNotification.show();
});
// 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)');
});
what was the wrong I am doing will any one suggest me, how to resolve this isssue
thanks in advace
Try changing your code as follows
var CloudPush = require('ti.cloudpush');
try {
CloudPush.retrieveDeviceToken({
success : function deviceTokenSuccess(e) {
deviceID = e.deviceToken;
CloudPush.enabled = true;
Ti.API.info(deviceID);
Ti.App.Properties.setString('deviceid', '' + deviceID);
},
error : function deviceTokenError(e) {
alert('Failed to register for push! ' + e.error);
}
});
} catch(e) {
alert('Error :' + e);
}
// Process incoming push notifications
CloudPush.addEventListener('callback', function(evt) {
//alert(evt.payload);
var alertNotification = Titanium.UI.createAlertDialog({
title : 'app',
message : evt.data.alert,
cancel : 1,
buttonNames : ['OK']
});
alertNotification.show();
});
// 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)');
});
I have removed the callback methods from your code.
And also please make sure that you're using the latest CloudPush module. Please check the TiApp.xml, if you have selected '*' as module version, change it to latest number (3.2.3 for me)