สอบถามปัญหา Push Notification ครับ

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา สอบถามปัญหา Push Notification ครับ

สอบถามปัญหา Push Notification ครับ
พี่นิกคับ
 
ผมทำตามที่ที่ได้โพส เรื่อง push notification ครับ
 
พอรันแล้วไม่มีอะไรเกิดขึ้นคับ
 
ทำถึงขั้นตอนสุดท้ายแล้ว
 
 
วิธีการทำสอบ ก็ให้อัพไฟล์นี้ขึ้น server แล้วกด รีเฟรส หร็อ รันไฟล์ 
ถ้าไม่มีข้อผิดพลาด ก็ จะได้รับข้อความเป็น push notification ที่เครื่องมือถือของเรา
เท่าที่รู้ เราสามารถส่งได้พร้อมกันสูงสุด 1000 regid
 
 
แต่ผลลัพที่ได้หน้าบราวเซอร์เป็นแบบนี้ครับ
 
 
Warning: json_encode() expects exactly 1 parameter, 2 given in C:AppServwwwstd_project54231_416pusher.php on line 71
JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0.
 
 
 
ผมใส่ key ตามที่ได้สมัคร GCM แล้วครับ แต่ก้อยังไม่ได้
 
ไม่ทราบว่าพี่พอจะมีวิธีแก้ไขไหมครับ
 
ขอบคุณครับ


Natjadee 110.164.182.xxx 02-12-2014 16:02:46

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
ก้อบโค้ดไฟล์ pusher.php ไม่สมบูรณ์หรือเปล่า ลองโหลดจากนี้แทนดู

https://www.ninenik.com/download/pusher.rar



ninenik 171.96.17.xxx 02-12-2014
 ความคิดเห็นที่ 2
เป็นแบบนี้คับ


Warning: Unexpected character in input: '' (ASCII=92) state=1 in C:AppServwwwstd_project54231_416pusher.php on line 36

Warning: json_encode() expects exactly 1 parameter, 2 given in C:AppServwwwstd_project54231_416pusher.php on line 71
JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0.




Natjadee 110.164.182.xxx 02-12-2014 16:37
 ความคิดเห็นที่ 3
ทดสอบที่ server เลย ที่เครื่องน่าจะไม่ได้


ninenik 171.96.17.xxx 02-12-2014
 ความคิดเห็นที่ 4
ผมลง server แล้วครับ

เกิด error อย่างที่ให้ดูครับ


Natjadee 110.164.182.xxx 02-12-2014 21:12
 ความคิดเห็นที่ 5

ลองอ่านบทความดีๆ เราข้าม หรือตก ส่วนไหนไปหรือเปล่า

ดูว่า ที่ server ในฐานข้อมูลมีการบันทึกค่า val ของ regid เครื่องหรือเปล่า 



ninenik 1.47.144.xxx 02-12-2014
 ความคิดเห็นที่ 6
มีการบันทึกลงฐานข้อมูลแล้วครับ

เด๋วผมจะลองอีกทีอย่างละเอียดครับ



Natjadee 110.164.182.xxx 02-12-2014 22:46
 ความคิดเห็นที่ 7
เกิดปัญหาเดิมเลยคับพี่นิก


Natjadee 110.164.182.xxx 02-12-2014 23:37
 ความคิดเห็นที่ 8
เราโพสแจ้งปัญหา ก็ช่วยอะไรไ่ได้ ต้องลิ้สอะไรสักอย่างมาประกอบ ไม่ลองโพสโค้ดหน้า test_pusth.php มาดูหน่อยหรอ
อันไหนเป็นค่าเฉพาะ ก็เปลี่ยนเป็น xxx ก่อนโพส  
และก็ url ที่เราเรียกใช้ ว่่าใช้ที่ server นี้คือแบบไหน ไม่ใช่ที่เครื่องแล้วรัน php ได้น่ะ อันนั้นคนละอัน

หรือง่ายสุด เอาค่า val หรือที่เก็บ regid มาใส่ในโค้ดทดสอบนี้ดู อันนี้แบบทดสอบส่งตรงๆ เลย ไม่ต้องไปดึงฐานข้อมูล

<?php
header("Content-type:text/html; charset=UTF-8");            
header("Cache-Control: no-store, no-cache, must-revalidate");         
header("Cache-Control: post-check=0, pre-check=0", false);      
// ส่วนตั้งค่าการใช้งาน push
include("pusher.php");
$apiKey = "xxxxxx";  // ค่าา api key จาก phonegap ตอนที่ 9
$pusher = new Pusher($apiKey);


$regId=array("xxxxxxxxxxxxx"); // ใส่ regid


// prep the bundle
$msg = array
(
    'message'   => 'ทดสอบ',
    'title'     => 'Push',
);
$pusher->notify($regId, $msg);
 echo "<pre>";
print_r($pusher->getOutputAsArray());
echo "</pre>";
?>


ninenik 1.47.17.xxx 02-12-2014
 ความคิดเห็นที่ 9


Natjadee 110.164.182.xxx 02-12-2014 23:50
 ความคิดเห็นที่ 10
server เป็นของสาขาวิชาครับ

ไม่ได้รันบน localhost เครื่องตัวเองครับ


นี้เป็นไฟล์ test_push.php
<?php
header("Content-type:text/html; charset=UTF-8");            
header("Cache-Control: no-store, no-cache, must-revalidate");         
header("Cache-Control: post-check=0, pre-check=0", false);      
// เชื่อมต่อกับฐานข้อมูล      
$link=mysql_connect("***********","***********","***********"); // เชื่อมต่อ Server        
mysql_select_db("***********");  // ติดต่อฐานข้อมูล        
mysql_query("set character set utf8"); // กำหนดค่า character set ที่จะใช้แสดงผล   
// ส่วนตั้งค่าการใช้งาน push
include("pusher.php");
$apiKey = "***********";  // ค่าา api key จาก phonegap ตอนที่ 9
$pusher = new Pusher($apiKey);


$regId=array();
$q="
SELECT * FROM regid
";
$qr=mysql_query($q);
while($rs=mysql_fetch_assoc($qr)){
	$regId[]=$rs['val'];
}


// prep the bundle
$msg = array
(
    'message'   => 'ทดสอบ',
    'title'     => 'Push',
);
$pusher->notify($regId, $msg);
 echo "<pre>";
print_r($pusher->getOutputAsArray());
echo "</pre>";
?>

นี้เป็นไฟล์ pusher.php คับ

<?php
class Pusher
{
    const GOOGLE_GCM_URL = 'https://android.googleapis.com/gcm/send';
 
    private $apiKey;
    private $proxy;
    private $output;
 
    public function __construct($apiKey, $proxy = null)
    {
        $this->apiKey = $apiKey;
        $this->proxy  = $proxy;
    }
 
    /**
     * @param string|array $regIds
     * @param string $data
     * @throws Exception
     */
    public function notify($regIds, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, self::GOOGLE_GCM_URL);
        if (!is_null($this->proxy)) {
            curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
        }
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders());
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getPostFields($regIds, $data));
 
        $result = curl_exec($ch);
        if ($result === false) {
            throw new Exception(curl_error($ch));
        }
 
        curl_close($ch);
 
        $this->output = $result;
    }
 
    /**
     * @return array
     */
    public function getOutputAsArray()
    {
        return json_decode($this->output, true);
    }
 
    /**
     * @return object
     */
    public function getOutputAsObject()
    {
        return json_decode($this->output);
    }
 
    private function getHeaders()
    {
        return array('Authorization: key=' . $this->apiKey,'Content-Type: application/json');
    }
 
    private function getPostFields($regIds, $data)
    {
        $fields = array(
            "registration_ids" => is_string($regIds) ? array($regIds) : $regIds,
            "data"=> is_string($data) ? array('message' => $data) : $data,
        );
        return json_encode($fields, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
    }
}
?>


Natjadee 110.164.182.xxx 03-12-2014 00:02
1 2 Next






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ