簡體   English   中英

使用php在Android中發送推送通知

[英]Send push notification in android using php

我正在向用戶發送消息。 該消息已成功發送。 但我沒有在android mobile中接收通知。 我有代碼:-

enter code here
public function send_android_notification($device,$message,$user,$sender_id,$reciever_id,$list_id,$username)  
{
        define( 'API_ACCESS_KEY', 'My key(i already embed)' );
        $registrationIds = array( $device );
        // prep the bundle
        $msg = array
        (
        'message'   => $message['Message']['message'],
        'user'=>$user,
        'sender_id' =>$sender_id,
        'reciever_id' =>$reciever_id,
        'list_id'=>$list_id,
        'username' =>$username
        );
        $fields = array
        (
            'registration_ids'  => $registrationIds,
            'data'          => $msg
        );

        $headers = array
        (
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );

        $ch = curl_init();
        curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
        curl_setopt( $ch,CURLOPT_POST, true );
        curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
        curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
        $result = curl_exec($ch );
        curl_close( $ch );
        echo $result;
    }

誰能幫我。 我給我輸出: 在此處輸入圖片說明

{“ multicast_id”:8119665273981115453,“成功”:0,“失敗”:1,“ canonical_ids”:0

?php 
function sendPushNotificationToGCM($device, $message,$user,$sender_id,$reciever_id,$list_id,$username) {
        //Google cloud messaging GCM-API url
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $device,
            'data' => $msg = array (
                  'message'   => $message['Message']['message'],
                  'user'=>$user,
                  'sender_id' =>$sender_id,
                  'reciever_id' =>$reciever_id,
                  'list_id'=>$list_id,
                  'username' =>$username
                  ),
        );

        $headers = array(
            'Authorization: key=youKey',
            '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_SSL_VERIFYHOST, 0);   
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);               
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        curl_close($ch);
        return $result;
    } ?>

請嘗試這個。 就我而言,這是可行的。 請嘗試一下。 並檢查您的設備ID是否在獲取正確的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM