简体   繁体   中英

Payload null in ACS for GCM

I am working on android GCM services for receiving push notification on device.

I am able to register for token. I am getting the notification only when app is open.

If app is closed or in background the app crashes with console msg as "Payload is null!" It means token is correct and push notification msg receives from server. But application is not handling it well.

I am not getting what wrong I am doing here.

Here is my code.

var CloudPush = require('ti.cloudpush');
CloudPush.addEventListener('trayClickLaunchedApp', function (evt) {
    alert('Tray Click Launched App: '+JSON.stringify(evt));
});
CloudPush.addEventListener('trayClickFocusedApp', function (evt) {
    alert('Tray Click Focused App: '+JSON.stringify(evt));
});
CloudPush.addEventListener('callback', function (evt) {
    alert("Callback: "+JSON.stringify(evt));
});
function deviceTokenSuccess(e) {
    Ti.API.info('Device Token: ' + e.deviceToken);
}
function deviceTokenError(e) {
    alert('Failed to register for push! ' + e.error);
}

I am using titanium SDk 5.0.2 & CloudPush 3.4.1 on my Nexus-6 with Android(6.0.1)

Can anyone help me to solve the issue.

I think this is because of the error in your server side code.Which server are you using, if it is php refer the following code

<?php
    /*
    Original code by @patrickjongmans
    Edited and tested by @ricardoalcocer

    Description:
    Originally posted at http://developer.appcelerator.com/question/140589/how-to-send-push-notifiaction-to-android-using-php-controled-acs-#254798

    @ricardoalcocer:
    My only change was to add the ti_ids to the POST_FIELDS
    */
    /*** SETUP ***************************************************/
    $key        = "<your_acs_app_key>";
    $username   = "<your_acs_admin_user>";
    $password   = "<your_acs_admin_password>";
    $to_ids     = "everyone";
    $channel    = "Allusers";
    $message    = "YOUR_MESSAGE";
    $title      = "YOUR_ANDROID_TITLE";
    $tmp_fname  = 'cookie.txt';
    $json       = '{"alert":"'. $message .'","title":"'. $title .'","vibrate":true,"sound":"default"}';

    /*** PUSH NOTIFICATION ***********************************/
    $post_array = array('login' => $username, 'password' => $password);

    /*** INIT CURL *******************************************/
    $curlObj    = curl_init();
    $c_opt      = array(CURLOPT_URL => 'https://api.cloud.appcelerator.com/v1/users/login.json?key='.$key,
                        CURLOPT_COOKIEJAR => $tmp_fname, 
                        CURLOPT_COOKIEFILE => $tmp_fname, 
                        CURLOPT_RETURNTRANSFER => true, 
                        CURLOPT_POST => 1,
                        CURLOPT_POSTFIELDS  =>  "login=".$username."&password=".$password,
                        CURLOPT_FOLLOWLOCATION  =>  1,
                        CURLOPT_TIMEOUT => 60);
// show debug message
//var_dump($c_opt);
    /*** LOGIN **********************************************/
    curl_setopt_array($curlObj, $c_opt); 
    $session = curl_exec($curlObj);     
// show debug message
//var_dump($session); 
    /*** SEND PUSH ******************************************/
    $c_opt[CURLOPT_URL]         = "https://api.cloud.appcelerator.com/v1/push_notification/notify.json?key=".$key; 
    $c_opt[CURLOPT_POSTFIELDS]  = "channel=".$channel."&payload=".$json."&to_ids=".$to_ids; 

    curl_setopt_array($curlObj, $c_opt); 
    $session = curl_exec($curlObj);     
// show debug message
 //var_dump($session);
    /*** THE END ********************************************/
    curl_close($curlObj);
?>

I have done acs push with both php and c#.I could reproduce the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM