1

Titanium SDK version 3.4.0.GA Targeting Android SDK: 20

Debugging on device running Android 4.4.4

Am able to retrieve deviceToken and can receive push notifications.

But the push notifications show up as an alert only when the application has focus

When the application does not have focus, I get an error that says the application has stopped working.

Sometimes, bringing the application back into focus shows the notifications sent when the application was not in focus.

How do start to debug this, please?

A little fogged - any pointers would be a great help

Many thanks

Iyer

// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
var df = require("./dreamfactory");
var promise = require("./Promise");

var winMain = Ti.UI.createWindow({
        backgroundColor:"#000",
        layout: "vertical"
});

var CloudPush = require('ti.cloudpush');
var deviceTokenLabel = Ti.UI.createLabel({
    top: '10dp', width: '320dp', height: (CloudPush.pushType=='gcm'?'150dp':'40dp'),
    font: { fontSize:14}, color: 'white',
    text: 'Device Token'
});
winMain.add(deviceTokenLabel);

var deviceToken = Ti.App.Properties.getString("deviceToken", "");
if (deviceToken == "") {
    CloudPush.retrieveDeviceToken({
        success: deviceTokenSuccess,
        error: deviceTokenError
    }); 
} else {
        deviceTokenLabel.text = 'Already Had Device Token:' + deviceToken;
        registerWithDSP();
}

function deviceTokenSuccess(e) {
    Ti.API.info('Device Token: ' + e.deviceToken);
    deviceTokenLabel.text = 'Device Token:' + e.deviceToken;
    Ti.App.Properties.setString('deviceToken', e.deviceToken);
    registerWithDSP();    
    //enablePush.enabled = true;
}

function registerWithDSP() {
    var body = {email:"iyer@ndtv.com" , password : "yeggikallie"};
    body = JSON.stringify(body);
    Ti.API.info(body);    
    df.makeRequest("post","/user/session", body).then(function(response) {
        SESSION_ID = response.session_id;
        Ti.API.info("session id " + SESSION_ID);
    }, function(error){
          Ti.API.info("Could not connect " + error);
    });

} 
function deviceTokenError(e) {
    alert('Failed to register for push! ' + e.error);
    deviceTokenLabel.text = 'Failed to get device token.';
}

CloudPush.addEventListener('callback', function (evt) {
    alert(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)');
});

winMain.open();
K Y Iyer
  • 315
  • 1
  • 4
  • 11

1 Answers1

-1

Depending on how you are sending your push notifications, it could be a malformed payload (which was my issue). I'm using PHP and my code is below:

$data = array(
    'title' => 'Your App Title',
    'sound' => 'default',
    'alert' => 'Your message here',
    'vibrate' => true );

$fields = array(
    'registration_ids'  => array('YOUR DEVICE TOKEN']),
    'data'              => array('payload'=> array('android' => $data)),
    'aps'               => $data );

$headers = array( 
        'Authorization: key=' . 'YOUR API KEY',
        'Content-Type: application/json'
    );

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);
troyfoley
  • 19
  • 3
  • **[You should not switch off `CURLOPT_SSL_VERIFYHOST` or `CURLOPT_SSL_VERIFYPEER`](https://paragonie.com/blog/2017/10/certainty-automated-cacert-pem-management-for-php-software)**. It could be a security risk! [Here is how to get the certificate bundle if your server is missing one](https://stackoverflow.com/a/32095378/1839439) – Dharman Mar 14 '20 at 23:13