Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Titanium Mobile Push Notifications callback not fired

I asked this question on the developers forum too, but as my time is running out I hoped posting on stackoverflow might be quicker.

Currently I am able to receive push notifications and I want to send some extra data along, after searching around I concluded that this should be possible. The idea is to set the messages from a users inbox (in the app) when a push notification is fired, so a users gets a push notification that says "New message" and along with it is the message send in a different key which is supposed to be saved in the Applications.Properties when the push notifications callback function is called.

However, the callback function is never triggered. The notifications are send, the badge is set to what my php script does so is the message. To test if the callback was triggered I changed the badge and message to hardcoded values (see below), I got this code from mainly from ios muncher but I assumed that the alert in the callback would be the one shown to the user.

A different thing I noticed, I only receive push notifications when the user is not using the app, so when it is running in the background. When a user is using the app push notifications are not shown, I thought that this might be because the callback is not triggered.

Thanks in advance for all kind help.

Below some code:

Titanium.Network.registerForPushNotifications({  
    types: [  
            Titanium.Network.NOTIFICATION_TYPE_BADGE,  
            Titanium.Network.NOTIFICATION_TYPE_ALERT,
            Titanium.Network.NOTIFICATION_TYPE_SOUND  
        ],  
        success:function(e){
            var deviceToken = e.deviceToken;  
            Ti.API.info("Push notification device token is: "+deviceToken);  

            //alert('device token is' +e.deviceToken);  
            var request = Titanium.Network.createHTTPClient();

            request.open("POST","http://*********/sendToken.php");  
            var params = {  
                "token": e.deviceToken,
                "username": authProperties[0].username,
                "userId": authProperties[0].userId    
            };  

            request.send(params);

            Ti.API.info("Push notification types:         "+Titanium.Network.remoteNotificationTypes);  
    Ti.API.info("Push notification enabled:"+Titanium.Network.remoteNotificationsEnabled);  
},  
error:function(e){
    alert("Error during registration: "+e.error);

    Ti.API.info("Error during registration: "+e.error);  
},
callback:function(e)  
{  
    // called when a push notification is received.  
    //Titanium.Media.vibrate();  
    var data = JSON.parse(e.data);

    request.open("POST","http://*********/callback.php");  
    var params = {  
        "token": e.deviceToken,
        "username": authProperties[0].username,
        "userId": authProperties[0].userId    
    };  

    request.send(params);

    //  Message data for the inbox
    var inboxData = data.inbox;     

    Titanium.App.properties.setString("badgeCount",data.badge);

    var badge = data.badge;  
    if(badge > 0){  
        Titanium.UI.iPhone.appBadge = 202;//badge;  
    }  

    var message = data.message;
    if(message != ''){  
        var my_alert=Ti.UI.createAlertDialog({title:'',message:JSON.stringify(inboxData) });

        my_alert.show();  
    }  
}  
});

The following script handles the php part of the push notification:

$serverId = "81273";  
    $name = "APNS";  
    $apnsPort = 2195;//5223;  
    $passPhrase = "";  
    $fwrite = "";  
    $sslUrl = "ssl://gateway.push.apple.com:" . $apnsPort;  
    $apnsCert = "./apns-distr.pem";//give the apns.pem file path on your server  
    $badge = 22;
$message = "[". date("d-m-Y h:i:s") . '] Er is een nieuw bericht voor u.';  
$inboxArray = array();
$inboxArray["id"]= 1;
$inboxArray["message"] = "Dit bericht dient als test";
$inboxArray["date"] = date("d-m-Y h:i:s");
$apnspayload['aps'] = array ('alert' => $message,'badge' => $badge,'sound' => 'default', 'inbox' => $inboxArray);  

$payload = json_encode($apnspayload);  

$tokens = array();
$tokens[] = "********** ** * * *";

foreach($tokens as $tokenId){


    $apnsMessage = chr(1) . pack('N', time()) . pack('N', time() + 86400) . chr(0) . chr(32) 
        . pack('H*', str_replace(' ', '', $tokenId)) . chr(0) . chr(strlen($payload)) . $payload;  

    $streamContext = stream_context_create();  

    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);  
    stream_context_set_option($streamContext, 'ssl', 'passphrase', $passPhrase);  

    $apns = stream_socket_client($sslUrl, $error, $errorString, 6, STREAM_CLIENT_CONNECT, $streamContext);  

    if($apns){  
        $fwrite = fwrite($apns, $apnsMessage);  

        fclose($apns);  
        @socket_close($apns);  
    }else{  
        echo 'request failed';  
    }  
}`
like image 427
Vincent Cohen Avatar asked Nov 04 '22 07:11

Vincent Cohen


1 Answers

Problem solved,

It seems something with the JSON went wrong, still not sure what.

In the App.js: var data = JSON.parse(e.data); upon removing the JSON.parse() the code worked. For some reason the JSON data did not need to be parsed.. thus the line became var data = e.data;

like image 194
Vincent Cohen Avatar answered Nov 10 '22 03:11

Vincent Cohen