Post Reply

Need help with GCM server [PHP]

OP pablo98123

2nd July 2014, 05:01 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
2 posts
Join Date:Joined: Jun 2014
Hi..Well, here I am..with a big problem..ok, not so big, but..for me it is. I'm trying to send push notification (with google cloud notification) from my PHP site, and i have a 401 response from them. I made a few changes like send registration ids I want to send notification, and all the thing keeps giving me troubles. I post the PHP script I use to send php notification (With the key alterated for obvious reasons) and I really hope you can help me in my..ignorance:

When I want to send a notification, depends on what type of notification I want to send, I call to
Quote:

notifyDbUpdate($eUser)

, where $eUser is the user that trigger push notification (so I don't need to say him nothing about this update) OR, when an app update occurs, I call to notifyAppUpdate()
PHP Code:
<?php

/**
 * Description of pushNotification
 *
 * @author pablo
 */
class PushNotification {
    
    
    private static 
$GCM_ACTION_UPDATE_DB "update_database";
    private static 
$GCM_ACTION_UPDATE_APP "update_database";    
    
    private static 
$url 'https://android.googleapis.com/gcm/send';
    private static 
$serverApiKey "KEY-SERVER-FROM-GCM";
    private static 
$instance null;
   
    function 
__construct()
    {
        
//No se construye lo no edificable ;)
    
}
    function 
__clone()
    {
        
//No me gustan los clones
    
}
   
    
/**
     * 
     * @return PushNotification
     */
    
public static function getInstance()
    {
        if (!isset(
self::$instance)) {
            
self::$instance = new self;
        }
        return 
self::$instance;
    }
    
    
/**
     * Send push notification to all dispositives
     */
    
private function send($data) {
       
        
$headers = array(
            
'Authorization: key=' $this->serverApiKey,
            
'Content-Type: application/json'
        
);
              
        
$ch curl_init();
       
        
curl_setopt($chCURLOPT_URLPushNotification::$url);
        
curl_setopt($chCURLOPT_POSTtrue);
        
curl_setopt($chCURLOPT_HTTPHEADER$headers);
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
curl_setopt($chCURLOPT_POSTFIELDSjson_encode($data));
              
        
$resultado curl_exec($ch);
       
        
curl_close($ch);
       
        
$this->registerAction($resultado);
    }
   
    private function 
registerAction($response)
    {
        if (!
$response) {//Si falló la consulta
            
            
$eLogLine = new LogLine();
            
$eLogLine->setResponse($response);
            
$eLogLine->setType(LogLine::LOG_TYPE_ERROR);
            
            
$dbm = new DBManager();
            
$dbm->log($eLogLine);            
        }
        else {
//Si hubo una respuesta
            
$eLogLine = new LogLine();
            
$eLogLine->setResponse($response);
            
$eLogLine->setType(LogLine::LOG_TYPE_UNDEFINED);
            
            
$dbm = new DBManager();
            
$dbm->log($eLogLine);            
        }
    }        
    
    public function 
notifyDbUpdate() {
        
        
$dbm = new DBManager();
        
        
$colGCMIds $dbm->getGCMReceivers(NULL);
        
        
$arrayIds ""//Comienzo armado cadena JSON de Id's
        
        
foreach ($colGCMIds as $Id) {
            
$arrayIds .= '"' $Id '",';
        }
        
$arrayIds trim($arrayIds,","); //Fin armado cadena JSON de Id's
        
        
        
$action PushNotification::$GCM_ACTION_UPDATE_DB;
        
        
$data = array(
            
'data' => array(
                
"action" => "{$action}",
                
"version"=> $dbm->getDbVersion()
            ),
            
"registration_ids" => array($arrayIds),
            
"collapse-key" => "db_update"
        
);
        
$this->send($data);
    }
    
    
/**
     * 
     * @param User $eUser
     */
    
public function notifyAppUpdate($eUser) {
        
        
$dbm = new DBManager();
        
        
$colGCMIds $dbm->getGCMReceivers($eUser);
        
        
$arrayIds ""//Comienzo armado cadena JSON de Id's
        
        
foreach ($colGCMIds as $Id) {
            
$arrayIds .= '"' $Id '",';
        }
        
$arrayIds trim($arrayIds,","); //Fin armado cadena JSON de Id's
        
        
$action PushNotification::$GCM_ACTION_UPDATE_APP;
        
        
$data = array(
            
'data' => array(
                
"action" => "{$action}",
                
"version"=> "{$dbm->getDbVersion()}"
            
),
            
"registration_ids" => array($arrayIds),
            
"collapse-key" => "app_update"
        
);
        
$this->send($data);
    }
    }
Thanks a lot for you help!
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Top Threads in Android Q&A, Help & Troubleshooting by ThreadRank