php
  • android
  • notifications
  • push-notification
  • 2014-05-06 83 views 0 likes 
    0

    有一個在我的Android應用程序通知.....但我看不到我的設備上的任何通知... 繼承人我的代碼...對設備沒有通知,但沒有在應用程序通知-Android推送通知

    function androidnotify($deviceToken,$message,$app,$badge,$alerttype,$xyzid) 
        { 
        $apiKey = "........."; 
        $url = 'https://android.googleapis.com/gcm/send'; 
        $regid[]=$deviceToken; 
        $registrationIDs = $regid; 
        $fields = array('registration_ids'=> $registrationIDs, 
               'data' => $message, 
            'badge'=> $badge, 
              'alerttype'=> $alerttype, 
           'xyzid' => $xyzid); 
    
        $headers = array('Authorization: key=' . $apiKey, 
          'Content-Type: application/json'); 
    
          $ch = curl_init(); 
          curl_setopt($ch, CURLOPT_URL, $url); 
          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)); 
          $result = curl_exec($ch); 
        } 
    

    我的設備需要太多的通知...我應該怎麼辦....

    回答

    1

    你可以從GCM的演示這個LINK

    它的工作與PHP代碼

    這裏是它的一個代碼

    $ registatoin_ids必須設備令牌(推令牌)$消息的陣列也必須是陣列

    define("GOOGLE_API_KEY","API_KEY"); 
    class GCM { 
         //put your code here 
         // constructor 
         function __construct() { 
    
         } 
    
         /** 
         * Sending Push Notification 
         */ 
         public function send_notification($registatoin_ids, $message) { 
          // include config 
          include_once 'config.php'; 
          // Set POST variables 
          $url = 'https://android.googleapis.com/gcm/send'; 
    
          $fields = array(
           'registration_ids' => $registatoin_ids, 
           'data' => $message, 
          ); 
    
          $headers = array(
           'Authorization: key=' . GOOGLE_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, $url); 
    
          curl_setopt($ch, CURLOPT_POST, true); 
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    
          // Disabling SSL Certificate support temporarly 
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    
          curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 
    
          // Execute post 
          $result = curl_exec($ch); 
          if ($result === FALSE) { 
           die('Curl failed: ' . curl_error($ch)); 
          } 
    
          // Close connection 
          curl_close($ch); 
          /*echo $result/*."\n\n".json_encode($fields);*/ 
         } 
    
        } 
    

    要發送GCM類的GCM創建對象

    $gcm = new GCM(); 
    

    並且您可以發送GCM,如

    $gcm->send_notification(DEVICE_TOKENS_AS_ARRAY, MESSAGE_AS_ARRAY); 
    
    相關問題